Base64解码不完整

huangapple go评论91阅读模式
英文:

Base64 Decode not complete

问题

我一直在尝试解码从其他系统返回的一些字符串,但是如果我使用在线解码器或者使用PHP解码,解码后的字符串结果是不同的。我尝试使用base64.StdEncoding.DecodeString()base64.StdEncoding.Decode(),但是结果仍然与在线解码器或PHP代码的结果不同。

这是我的代码:

// 编码后的字符串
s := "HxJVICMcHCYeHUJcEWISWk09JgxHBwRPXHlHWFoGf1JXCkgFS1xXVU9Ze1FSCUwUIFMdGx0-SglVDmRNUHdAJRETRR0hHx0aIB1OExcgUCkeRyEaDUE5Y1oTREhcCRJSXAtWHBwfJSUYFUALUFd-VlkIU0JYFg5TA04PURA_OFBQXHpXWVxTSEZlC1FZTH4TKTUXGx1ORBcfSB0fO0xRJRoRVxsmExcoIhxEAxsQfF9bD1tXSQd-XEkBShEoPH80UWEJWAhAQhA0XgNVTwdWZxEX"

// 在字符串右侧添加'='
strHelper := string2.NewStringHelper()
sl := math.Ceil(float64(len(s) / 4 * 4))
sp := strHelper.StrPadRight(s, "=", int(sl))
sp = strtr(sp, map[string]string{"-_": "+/"})
  
// 使用base64.StdEncoding.DecodeString()解码字符串
spb, _ := base64.StdEncoding.DecodeString(sp)
  
// 使用base64.StdEncoding.Decode()解码字符串
b64 := make([]byte, base64.StdEncoding.DecodedLen(len(sp)))
n, err := base64.StdEncoding.Decode(b64, []byte(sp))

// 使用base64.NewDecoder()解码字符串
b64s := strings.NewReader(sp)
b64d := base64.NewDecoder(base64.StdEncoding, b64s)
buf := new(strings.Builder)
io.Copy(buf, b64d)

// 解码后的字符串结果
U #&B\bZM=&GO\yGXZRW
HK\WUOY{QR	L S

// 在线解码器或PHP的结果
U #&B\bZM=&GO\yGXZRW
HK\WUOY{QR	L S>J	UdMPw@%E! N P)G!
A9cZDH\	R\V%%@PW~VYSBXSNQ?8PP\zWY\SHFeQYL~)5NDH;LQ%W&("D|_[[WI~\IJ(<4Qa	X@B4^UOVg

https://go.dev/play/p/Yqf2I2QgWxS

请问有人可以告诉我我的代码哪里出错了吗?

英文:

I have bee trying to decode some string returned from other system, but the decoded string have different result if i decode with online decoder or using PHP. I've been trying using base64.StdEncoding.DecodeString() and base64.StdEncoding.Decode() but the result still not the same as the result of online decoder or PHP code.

Here is my code:

// encoded string
s := "HxJVICMcHCYeHUJcEWISWk09JgxHBwRPXHlHWFoGf1JXCkgFS1xXVU9Ze1FSCUwUIFMdGx0-SglVDmRNUHdAJRETRR0hHx0aIB1OExcgUCkeRyEaDUE5Y1oTREhcCRJSXAtWHBwfJSUYFUALUFd-VlkIU0JYFg5TA04PURA_OFBQXHpXWVxTSEZlC1FZTH4TKTUXGx1ORBcfSB0fO0xRJRoRVxsmExcoIhxEAxsQfF9bD1tXSQd-XEkBShEoPH80UWEJWAhAQhA0XgNVTwdWZxEX"
// add '=' on the right string
strHelper := string2.NewStringHelper()
sl := math.Ceil(float64(len(s) / 4 * 4))
sp := strHelper.StrPadRight(s, "=", int(sl))
sp = strtr(sp, map[string]string{"-_": "+/"})
// decode the string using base64.StdEncoding.DecodeString()
spb, _ := base64.StdEncoding.DecodeString(sp)
// decode the string using base64.StdEncoding.Decode()
b64 := make([]byte, base64.StdEncoding.DecodedLen(len(sp)))
n, err := base64.StdEncoding.Decode(b64, []byte(sp))
// decode the string using base64.NewDecoder()
b64s := strings.NewReader(sp)
b64d := base64.NewDecoder(base64.StdEncoding, b64s)
buf := new(strings.Builder)
io.Copy(buf, b64d)
// result of decoded string
U #&B\bZM=&GO\yGXZRW
HK\WUOY{QR	L S

// result from online decoder or PHP
U #&B\bZM=&GO\yGXZRW
HK\WUOY{QR	L S>J	UdMPw@%E! N P)G!
A9cZDH\	R\V%%@PW~VYSBXSNQ?8PP\zWY\SHFeQYL~)5NDH;LQ%W&("D|_[[WI~\IJ(<4Qa	X@B4^UOVg

https://go.dev/play/p/Yqf2I2QgWxS

Please can someone enlightened me where my code is wrong?

答案1

得分: 0

在Go语言中,永远不要忽略错误!如果你进行检查:

data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
    panic(err)
}

输出结果应该是:

panic: illegal base64 data at input byte 71

你的输入不是标准的Base64字符集。标准字符集使用+/字符。而你的输入是使用修改过的字符集构建的,这个字符集在URL中用于传输编码值,它使用-_字符。

所以,使用base64.URLEncoding进行解码:

data, err := base64.URLEncoding.DecodeString(s)
if err != nil {
    panic(err)
}

fmt.Println(string(data))

这样就不会产生错误了(在Go Playground上试一试)。

如果你的输入不包含填充字符,可以使用base64.RawURLEncoding

英文:

Never omit errors in Go! If you check it:

data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}

Output:

panic: illegal base64 data at input byte 71

Your input is constructed not from the standard Base64 charset. The standard charset uses the + and / characters. Your input is constructed using a modified charset, a charset safe and used to transmit encoded values in URLs which uses - and _.

So decode using base64.URLEncoding:

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

This will not result in an error (try it on the Go Playground).

If your input does not contain the padding characters, then use base64.RawURLEncoding.

huangapple
  • 本文由 发表于 2022年6月17日 16:47:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/72656662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定