简体   繁体   English

用空格解码 base64

[英]Decode base64 with white space

I have a base64 encoded string i'm trying to decrypt with go.我有一个 base64 编码的字符串,我想用 go 解密。 The string contains white spaces which should be ignored.该字符串包含应被忽略的空格。 A sample code I'm trying:我正在尝试的示例代码:

s := "eyJ0aHJlZURTU2VydmVyVHJhbnNJRCI6IjEzZmU3MWQ0LWQxMGQtNDIyMC1hMjE2LTIwMDZkMWRkNGNiOCIsImFjc1RyY++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++W5zSUQiOiJkN2M0NWY5OS05NDc4LTQ0YTYtYjFmMi0xMDAwMDAwMDMzNjYiLCJtZXNzYWdlVHlwZSI6IkNSZXEiLCJtZXNzYWdlVmVyc2lvbiI6IjIuMS4wIiwiY2hhbGxlbmdlV2luZG93U2l6ZSI6IjAyIn0%3D"

out, err := base64.URLEncoding.DecodeString(s)
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(string(out))

This code returns:此代码返回:

illegal base64 data at input byte 93输入字节 93 处的非法 base64 数据


After changing the string padding, and using StdEncoding instead of URLEncoding:更改字符串填充后,并使用 StdEncoding 而不是 URLEncoding:

s= strings.Replace(s, "%3D", "=", -1)
out, err := base64.StdEncoding.DecodeString(s)
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(string(out))

The output will be:输出将是:

{"threeDSServerTransID":"13fe71d4-d10d-4220-a216-2006d1dd4cb8","acsTrc nsID":"d7c45f99-9478-44a6-b1f2-100000003366","messageType":"CReq","messageVersion":"2.1.0","challengeWindowSize":"02"} {"threeDSServerTransID":"13fe71d4-d10d-4220-a216-2006d1dd4cb8","acsTrc nsID":"d7c45f99- 9478-44a6-b1f2-100000003366","messageType":"CReq","messageVersion":"2.1.0","challengeWindowSize":"02"}


How can I decrypt the string correctly?如何正确解密字符串?

What you have is most likely "cut off" from a URL, and it is in URL-encoded form.你所拥有的很可能是从一个 URL 中“截断”的,它是 URL 编码的形式。 So to get a Base64 string, you have to first decode it, you may use url.PathUnescape() for this.因此,要获得 Base64 字符串,您必须先对其进行解码,您可以url.PathUnescape()使用url.PathUnescape()

Once you have the unescaped string, you may decode it using the base64.StdEncoding encoder.获得未转义的字符串后,您可以使用base64.StdEncoding编码器对其进行解码。 Note that just because it is / was part of a URL, that doesn't make it a base64 string that used the alphabet of the URL-safe version of Base64.请注意,仅仅因为它是 / 是 URL 的一部分,并不能使其成为使用 Base64 的 URL 安全版本的字母表的 base64 字符串。

Also the + signs in the middle of it are really just "junk".而且中间的+号真的只是“垃圾”。 They shouldn't be there in the first place, so double-check how you get your input, but now that they are there, you have to remove them.它们一开始就不应该在那里,所以请仔细检查您是如何获得输入的,但是现在它们在那里,您必须将其删除。 For that, you may use strings.Replace() .为此,您可以使用strings.Replace()

Final code to decode your invalid input:解码无效输入的最终代码:

s := "eyJ0aHJlZURTU2VydmVyVHJhbnNJRCI6IjEzZmU3MWQ0LWQxMGQtNDIyMC1hMjE2LTIwMDZkMWRkNGNiOCIsImFjc1RyY++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++W5zSUQiOiJkN2M0NWY5OS05NDc4LTQ0YTYtYjFmMi0xMDAwMDAwMDMzNjYiLCJtZXNzYWdlVHlwZSI6IkNSZXEiLCJtZXNzYWdlVmVyc2lvbiI6IjIuMS4wIiwiY2hhbGxlbmdlV2luZG93U2l6ZSI6IjAyIn0%3D"
s = strings.Replace(s, "+", "", -1)
var err error
if s, err = url.PathUnescape(s); err != nil {
    panic(err)
}

out, err := base64.StdEncoding.DecodeString(s)
if err != nil {
    panic(err)
}
fmt.Println(string(out))

Complete output (try it on the Go Playground ):完整的输出(在Go Playground上试试):

{"threeDSServerTransID":"13fe71d4-d10d-4220-a216-2006d1dd4cb8",
   "acsTransID":"d7c45f99-9478-44a6-b1f2-100000003366","messageType":"CReq",
    "messageVersion":"2.1.0","challengeWindowSize":"02"}

Note that the + sign is a valid symbol in the alphabet of the standard Base64, and you can even decode the Base64 without removing the + symbols, but then you get junk data remaining in the JSON keys in the result.请注意, +符号是标准 Base64 字母表中的有效符号,您甚至可以在不删除+符号的情况下对 Base64 进行解码,但随后您会在结果的 JSON 键中得到剩余的垃圾数据。

The input string has three problems输入字符串存在三个问题

First the + signs in the middle of it首先是中间的+号

Second there is garbage (a url encoded +) at the end其次,末尾有垃圾(一个 url 编码 +)

Third the string appears to not be valid Base64第三,字符串似乎不是有效的 Base64

To remove the plus signs in the middle, find the index of the start and finish and make a new string To remove the garbage at the end, terminate the string earlier ( at index 249 of the fixed string)去除中间的加号,找到开始和结束的索引并创建一个新字符串去除末尾的垃圾,提前终止字符串(在固定字符串的索引249处)

There is a further problem with the string at index 148 of the fixed string, which I would guess is due to bad data固定字符串的索引 148 处的字符串还有一个问题,我猜这是由于数据错误

But the code fragment below shows how to overcome the first two things但是下面的代码片段显示了如何克服前两件事

package main

import (
    "fmt"
    "encoding/base64"
    "strings"
)

func main() {
    s := "eyJ0aHJlZURTU2VydmVyVHJhbnNJRCI6IjEzZmU3MWQ0LWQxMGQtNDIyMC1hMjE2LTIwMDZkMWRkNGNiOCIsImFjc1RyY++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++W5zSUQiOiJkN2M0NWY5OS05NDc4LTQ0YTYtYjFmMi0xMDAwMDAwMDMzNjYiLCJtZXNzYWdlVHlwZSI6IkNSZXEiLCJtZXNzYWdlVmVyc2lvbiI6IjIuMS4wIiwiY2hhbGxlbmdlV2luZG93U2l6ZSI6IjAyIn0%3D"


    a:=strings.Index(s,"+")
    b:=strings.LastIndex(s,"+")+1

    fixed:=s[0:a] + s[b:249]
    out, err := base64.StdEncoding.DecodeString(fixed)
    if err != nil {
        fmt.Println(err)
        fmt.Println(fixed)

    }
    fmt.Println(a,b)
    fmt.Println(String(out))

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM