douhui3305 2017-02-26 18:56
浏览 76
已采纳

GOLANG 64decode字符串非法数据,不确定为什么

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: illegal base64 data at input byte 408

Below is the code as well, its pretty short:

package main

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

func main() {

    fmt.Println("Hello, playground and stackover flow 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`


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



 fragments := strings.Split(token,".")
fmt.Println ( "Here is the value
"+fragments[1])

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

fmt.Println (err)

fmt.Println ("any ideas")

}

  • 写回答

1条回答 默认 最新

  • dougong2306 2017-02-26 19:35
    关注

    JWT's are base64 URL encoded and not padded.

    Here's a fixed version on play

    Only difference is I used base64.RawURLEncoding

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作