在Go语言中对字符串进行Base64解码。

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

Doing base64 decoding on a string in Go

问题

我需要在Go语言中对一个特定的字符串进行base64解码。这个字符串看起来像这样:

qU4aaakFmjaaaaI5aaa\/EN\/aaa\/SaaaJaaa6aa+nGnk=

请注意,这不是完全相同的字符串,但它具有相同的形状和字符数,填充字符,并且在字符串中的相同位置有那些\/

我们称之为"key"。

在PHP中,如果我运行

base64_decode($key);

解码操作是成功的。

如果在Python中运行

base64.b64decode(key)

解码操作也是成功的。问题是,我无法在Go中对这个字符串进行base64解码。

dcd, err := base64.StdEncoding.DecodeString("qU4aaakFmjaaaaI5aaa\\/EN\\/aaa\\/SaaaJaaa6aa+nGnk=")
if err != nil {
	log.Fatal(err)
}
return dcd

这将返回错误信息:

illegal base64 data at input byte 19

在Go版本中,我必须转义这些反斜杠。看起来错误出现在第19个字节。考虑到我用作示例的字符串与实际引发问题的字符串具有相同的长度,我认为错误发生在带有\字符的字节处。我该怎么办呢?

英文:

I have a particular string that I need to run base64 decode on in Go. This string looks something like this:

qU4aaakFmjaaaaI5aaa\/EN\/aaa\/SaaaJaaa6aa+nGnk=

Please note this is not the exact same string but it does have the same shape and number of characters, padding characters and it has those \/ things on the same positions in the string.

Let's call it key.

In PHP if I run

base64_decode($key);

the decode operation is successful

If In Python I run

base64.b64decode(key)

the decode operation is once more successful. Problem is, I can't do base64 decoding on this thing in Go.

dcd, err := base64.StdEncoding.DecodeString("qU4aaakFmjaaaaI5aaa\\/EN\\/aaa\\/SaaaJaaa6aa+nGnk=")
if err != nil {
	log.Fatal(err)
}
return dcd

This will return the error

illegal base64 data at input byte 19

In the Go version, I have to escape those backslashes. It seems that the error appears at byte 19. Bearing in mind that this string that I am using as an example has the same length as the string that is actually causing the problem I would believe that the error happens right at the byte with the \ character. What can I do about this?

答案1

得分: 2

标准Base64字母表不包含反斜杠。因此,qU4aaakFmjaaaaI5aaa\/EN\/aaa\/SaaaJaaa6aa+nGnk=输入不是有效的Base64编码字符串。

正斜杠是Base64中的有效字符,只是反斜杠不是。可能\/是指代单个斜杠的序列。如果是这样,请将\/序列替换为单个/,然后就可以使用了。

例如:

s := `qU4aaakFmjaaaaI5aaa\/EN\/aaa\/SaaaJaaa6aa+nGnk=`
s = strings.ReplaceAll(s, `\/`, `/`)
dcd, err := base64.StdEncoding.DecodeString(s)
if err != nil {
	log.Fatal(err)
}

fmt.Println(string(dcd))

输出结果为(在Go Playground上尝试):

�Ni��6�i�9i����i��i��i��i��y

如果\/不是特殊序列,并且您想要丢弃输入中的所有无效字符,可以按照以下方式完成:

var valid = map[rune]bool{}

func init() {
	for _, r := range "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" {
		valid[r] = true
	}
}

func clean(s string) string {
	return strings.Map(func(r rune) rune {
		if valid[r] {
			return r
		}
		return -1
	}, s)
}

func main() {
	s := `qU4aaakFmjaaaaI5aaa\/EN\/aaa\/SaaaJaaa6aa+nGnk=`
	s = clean(s)
	dcd, err := base64.StdEncoding.DecodeString(s)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(dcd))
}

输出结果相同。在Go Playground上尝试这个代码。

英文:

The alphabet of the standard Base64 does not contain backslash. So the qU4aaakFmjaaaaI5aaa\/EN\/aaa\/SaaaJaaa6aa+nGnk= input is not valid Base64 encoded string.

The forward slash is valid character in Base64, just not the backslash. It's possible the \/ is a sequence designating a single slash. If so, replace the \/ sequences with a single / and you're good to go.

For example:

s := `qU4aaakFmjaaaaI5aaa\/EN\/aaa\/SaaaJaaa6aa+nGnk=`
s = strings.ReplaceAll(s, `\/`, `/`)
dcd, err := base64.StdEncoding.DecodeString(s)
if err != nil {
	log.Fatal(err)
}

fmt.Println(string(dcd))

Which outputs (try it on the Go Playground):

�Ni��6�i�9i����i��i��i��i��y

If \/ is not a special sequence and you want to discard all invalid characters from the input, this is how it could be done:

var valid = map[rune]bool{}

func init() {
	for _, r := range "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" {
		valid[r] = true
	}
}

func clean(s string) string {
	return strings.Map(func(r rune) rune {
		if valid[r] {
			return r
		}
		return -1
	}, s)
}

func main() {
	s := `qU4aaakFmjaaaaI5aaa\/EN\/aaa\/SaaaJaaa6aa+nGnk=`
	s = clean(s)
	dcd, err := base64.StdEncoding.DecodeString(s)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(dcd))
}

Output is the same. Try this one on the Go Playground.

huangapple
  • 本文由 发表于 2021年6月7日 02:12:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/67862281.html
匿名

发表评论

匿名网友

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

确定