GOLANG 64decode 字符串非法数据,不确定原因。

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

GOLANG 64decode string illegal data, not sure why

问题

亲爱的Gophers:

我遇到了一个简单的问题。我正在尝试解码一个64编码的字符串(确切地说是OAUTH令牌)。

我将字符串拆分,并将拆分的部分之一进行64解码,但总是发现一个非法字符。当我将相同的值放入任何基于Web的64解码器中时,它可以正常工作。

我认为可能存在一个尾随的空字符或其他问题。有人可以给我一些建议吗?我在这里放置了代码:https://play.golang.org/p/cf-x6GgpEp

确切的错误是:illegal base64 data at input byte 408

以下也是代码,非常简短:

package main

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

func main() {

	fmt.Println("Hello, playground and stackoverflow users")

	token := `eyJhbGciOiJSUzI1NiIsImtpZCI6IkNKc2hmNnhMOGVuODlhVVZVenJaX1JqMTJwS3pqZkFUdkNqcT    lNbUxGTlUifQ.eyJ2ZXIiO
		jEsImp0aSI6IkFULlFxN21mck94RnhZaVFXZWpGQjczMTZnRXdQbWJBVGtsVzB6Sm1FWVRPU0UiLCJp    c3MiOiJodHRwczovL2Nvb
		XBhbnl4Lm9rdGEuY29tL29hdXRoMi9hdXMxYTRpYmRhdDBKWXc1czFkOCIsImF1ZCI6Imh0dHA6Ly9s    b2NhbGhvc3Q6MzAwMCIsI
		mlhdCI6MTQ4ODA2MDgxMywiZXhwIjoxNDg4MDY0NDEzLCJjaWQiOiIzam1OdlZDRlo1RjZsV096SU9O    TyIsInVpZCI6IjAwdTFhOXNrMXloenR3Y3FSMWQ4Iiwic2NwIjpbInJlYWQiLCJyZW1vdmUiLCJyZXN    lcnZlIl0sInN1YiI6InJlbW92ZUB3b3cuY29tIn0
		.XFGZoVv-w4kRgqMDB-Me1RM1aO5f5JH8W_fLFGnYzkLXGassbKMWafAfVx9j-    wzaWVFfHtsH4fiLjZZbCRYkWli0b6Q3VjdDPJq
		YcvhN_wQ9WTmqoJLNyY-    AA5o7yThL4Vz5ih7P_nJWMN7xEWUpWadeJbShvPNtdFHHPBFTAiDx0fXFs98vZHfeLEy2dnwYf4aY4p    w
		jcfmhOZNaVahMtQh0dJVa0ai4KPMqQXDXMHDrbEwa2eWY_2pCPXUZekvC3DyRFeSfkRJ6fIe3d6XccDgAy_qzjBCjnutyezg7ZzV
		d866wkb2j2kJwyr0w3Q5iBBFgxEv6vpcpaM_KHSXTyA`

	// 将令牌拆分以获取中间值 xx。(我们需要这个)。xx

	fragments := strings.Split(token, ".")
	fmt.Println("这是值\n" + fragments[1])

	jwtFragment, err := b64.StdEncoding.DecodeString(fragments[1])
	_ = jwtFragment //

	fmt.Println(err)

	fmt.Println("有什么想法")

}

希望对你有所帮助!

英文:

Fellow Gophers:

I'm stuck on something simple. I'm trying to convert decode a 64endoded string (OAUTH token to be exact).

I split the string, and put one of the parts of the split to be 64decoded, and I always find an illegal character. When I put the same value into any web based 64decoder it works.

I think that maybe there is a trailing null or something. Can anyone give me some advice. I put the code here: https://play.golang.org/p/cf-x6GgpEp

The exact error is: <code>illegal base64 data at input byte 408</code>

Below is the code as well, its pretty short:

package main

import (
	&quot;fmt&quot;
	&quot;strings&quot;
	b64 &quot;encoding/base64&quot;
)

func main() {

	fmt.Println(&quot;Hello, playground and stackover flow users&quot;)


	token := `eyJhbGciOiJSUzI1NiIsImtpZCI6IkNKc2hmNnhMOGVuODlhVVZVenJaX1JqMTJwS3pqZkFUdkNqcT    lNbUxGTlUifQ.eyJ2ZXIiO
jEsImp0aSI6IkFULlFxN21mck94RnhZaVFXZWpGQjczMTZnRXdQbWJBVGtsVzB6Sm1FWVRPU0UiLCJp    c3MiOiJodHRwczovL2Nvb
XBhbnl4Lm9rdGEuY29tL29hdXRoMi9hdXMxYTRpYmRhdDBKWXc1czFkOCIsImF1ZCI6Imh0dHA6Ly9s    b2NhbGhvc3Q6MzAwMCIsI
mlhdCI6MTQ4ODA2MDgxMywiZXhwIjoxNDg4MDY0NDEzLCJjaWQiOiIzam1OdlZDRlo1RjZsV096SU9O    TyIsInVpZCI6IjAwdTFhOXNrMXloenR3Y3FSMWQ4Iiwic2NwIjpbInJlYWQiLCJyZW1vdmUiLCJyZXN    lcnZlIl0sInN1YiI6InJlbW92ZUB3b3cuY29tIn0
.XFGZoVv-w4kRgqMDB-Me1RM1aO5f5JH8W_fLFGnYzkLXGassbKMWafAfVx9j-    wzaWVFfHtsH4fiLjZZbCRYkWli0b6Q3VjdDPJq
YcvhN_wQ9WTmqoJLNyY-    AA5o7yThL4Vz5ih7P_nJWMN7xEWUpWadeJbShvPNtdFHHPBFTAiDx0fXFs98vZHfeLEy2dnwYf4aY4p    w
jcfmhOZNaVahMtQh0dJVa0ai4KPMqQXDXMHDrbEwa2eWY_2pCPXUZekvC3DyRFeSfkRJ6fIe3d6XccDgAy_qzjBCjnutyezg7ZzV
d866wkb2j2kJwyr0w3Q5iBBFgxEv6vpcpaM_KHSXTyA`


// Spit the token to get the middle value xx.(we want this).xx



 fragments := strings.Split(token,&quot;.&quot;)
fmt.Println ( &quot;Here is the value\n&quot;+fragments[1])

jwtFragment, err := b64.StdEncoding.DecodeString(fragments[1])
_=jwtFragment //

fmt.Println (err)

fmt.Println (&quot;any ideas&quot;)

}

答案1

得分: 0

JWT(JSON Web Tokens)是进行 base64 URL 编码的,而且没有填充。

这里是修复后的版本

唯一的区别是我使用了 base64.RawURLEncoding

英文:

JWT's are base64 URL encoded and not padded.

Here's a fixed version on play

Only difference is I used base64.RawURLEncoding

huangapple
  • 本文由 发表于 2017年2月27日 02:56:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/42472754.html
匿名

发表评论

匿名网友

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

确定