dongshukou0240 2015-11-14 01:43
浏览 161
已采纳

开始:Base64解码有什么问题

I tried to decode a valid (based on my understanding) base64 encoded string in Go with:

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

A full example is here. I have a string "eyJlbWFpbF9hZGRyZXNzIjoiIiwiZXhwIjoxNDQ3NzIzMzY4LCJmaXJzdG5hbWUiOiIiLCJpYXQiOjE0NDc0NjQxNjgsImlzcyI6Imh0dHA6Ly91ZGFjaXR5LmNvbSIsImtpZCI6ImE3ZTg5ZWQyMSIsImxhc3RuYW1lIjoiIiwidXNlcl9pZCI6IjEyMzQ1Njc4IiwidXNlcm5hbWUiOiJoYW5zb2xvQGhvdGguY29tIn0", which can be properly decoded for example here or even in your browser's console with atob(that_string);, but for some reason go complains with:

illegal base64 data at input byte 236

Notice, that I can decode some other strings. So why can not I base64decode a valid encoded string in Go?

  • 写回答

1条回答 默认 最新

  • duanfeng3879 2015-11-14 01:47
    关注

    Your input does not have any padding. Therefore, you should use base64.RawStdEncoding over base64.StdEncoding:

    data, err := base64.RawStdEncoding.DecodeString(s)
    

    Example: https://play.golang.org/p/ZWfzYXQ5Ye

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?