英文:
How to URL Safe decode Base64 in Go, encoded in Perl?
问题
我有一个URL安全编码的字符串(由Perl生成),我需要在Go中解码它。
这里有两个程序,一个是Perl,一个是Go - Perl可以正常工作,但是Go会出错。
我无法理解这个问题。
请帮忙!
=== Perl代码 - 正常工作
#!/usr/bin/env perl
use common::sense;
use MIME::Base64::URLSafe;
my $str = 'Oi6cQzmolrUhkgHsNehtj9p_OsasB_6CIeygK0owoxTsXCtVWyQi-7DXxIJiaV-kSc6PGNC6uNz5V0A9QOGCaeCy6PolQY2Lt_v4JM42VEbsuML8guHfMO0ydvbXVcCR-yLfkz5CO0f-P1hVqxJBD8qPvk1t1DRzqmHP41DSfIm_WzlhtITnd_Wjt6E3CFS78HL3XjJlM-QBW9Z_GZgic8y7TlOWFzCRUf2Q-EZschrDi9l81E93XBNKe8knInL_uFN_oK_ob7fjnkGJO54RNn3coVsrzuIoNa6AI6oWLfsaJ5NyQYor5P0';
say urlsafe_b64decode($str);
=== Go代码 - 出错
package main
import (
b64 "encoding/base64"
"fmt"
)
func main() {
str := "Oi6cQzmolrUhkgHsNehtj9p_OsasB_6CIeygK0owoxTsXCtVWyQi-7DXxIJiaV-kSc6PGNC6uNz5V0A9QOGCaeCy6PolQY2Lt_v4JM42VEbsuML8guHfMO0ydvbXVcCR-yLfkz5CO0f-P1hVqxJBD8qPvk1t1DRzqmHP41DSfIm_WzlhtITnd_Wjt6E3CFS78HL3XjJlM-QBW9Z_GZgic8y7TlOWFzCRUf2Q-EZschrDi9l81E93XBNKe8knInL_uFN_oK_ob7fjnkGJO54RNn3coVsrzuIoNa6AI6oWLfsaJ5NyQYor5P0"
fmt.Println("source B64:", str)
_, err := b64.URLEncoding.DecodeString(str)
if err != nil {
fmt.Println("error b64:", err)
}
}
=== 结束
由于解码返回的结果被截断,因此无法在下一步中使用它。
英文:
I have an URL Safe encoded string (produced by Perl), that I need to decode in Go.
Here are two programs, in Perl and in Go - Perl works fine, but Go rises error.
I can't understand it.
Please help!
=== Perl code - works fine
#!/usr/bin/env perl
use common::sense;
use MIME::Base64::URLSafe;
my $str = 'Oi6cQzmolrUhkgHsNehtj9p_OsasB_6CIeygK0owoxTsXCtVWyQi-7DXxIJiaV-kSc6PGNC6uNz5V0A9QOGCaeCy6PolQY2Lt_v4JM42VEbsuML8guHfMO0ydvbXVcCR-yLfkz5CO0f-P1hVqxJBD8qPvk1t1DRzqmHP41DSfIm_WzlhtITnd_Wjt6E3CFS78HL3XjJlM-QBW9Z_GZgic8y7TlOWFzCRUf2Q-EZschrDi9l81E93XBNKe8knInL_uFN_oK_ob7fjnkGJO54RNn3coVsrzuIoNa6AI6oWLfsaJ5NyQYor5P0';
say urlsafe_b64decode($str);
=== Go code - rises error
package main
import (
b64 "encoding/base64"
"fmt"
)
func main() {
str := "Oi6cQzmolrUhkgHsNehtj9p_OsasB_6CIeygK0owoxTsXCtVWyQi-7DXxIJiaV-kSc6PGNC6uNz5V0A9QOGCaeCy6PolQY2Lt_v4JM42VEbsuML8guHfMO0ydvbXVcCR-yLfkz5CO0f-P1hVqxJBD8qPvk1t1DRzqmHP41DSfIm_WzlhtITnd_Wjt6E3CFS78HL3XjJlM-QBW9Z_GZgic8y7TlOWFzCRUf2Q-EZschrDi9l81E93XBNKe8knInL_uFN_oK_ob7fjnkGJO54RNn3coVsrzuIoNa6AI6oWLfsaJ5NyQYor5P0"
fmt.Println("source B64:", str)
_, err := b64.URLEncoding.DecodeString(str)
if err != nil {
fmt.Println("error b64:", err)
}
}
=== END
As decode returns truncated result it is impossible to use it in next step.
答案1
得分: 11
当没有填充时,请使用RawURLEncoding:
_, err := b64.RawURLEncoding.DecodeString(str)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论