[英]difference between python and node base64 decoding
I am puzzled at this base64 decoding issue, and it seems that python and node.js does this differently. 我对这个base64解码问题感到困惑,似乎python和node.js的做法有所不同。 Node does this correctly I believe.
我相信Node可以正确做到这一点。 Could anyone help point out why python does not work here?
谁能帮助指出python为什么在这里不起作用?
Thank you. 谢谢。
Node 节点
> console.log(Buffer.from('Im3Osc6_z4HPgc-J==', 'base64').toString());
"mαορρω
Python 蟒蛇
>>> from base64 import decodestring
>>> print decodestring('Im3Osc6_z4HPgc-J==')
"mαγ?s?p
What you provided is actually not a standard base64, but a URL-safe base64 您提供的实际上不是标准的base64,而是URL安全的base64
which substitutes
-
instead of+
and_
instead of/
in the standard Base64 alphabet"在标准Base64字母中用
-
代替+
,用_
代替/
”
To decode it in Python you need to use base64.urlsafe_b64decode
. 要在Python中对其进行解码,您需要使用
base64.urlsafe_b64decode
。
>>> import base64
>>> base64.urlsafe_b64decode('Im3Osc6_z4HPgc-J==')
'"m\xce\xb1\xce\xbf\xcf\x81\xcf\x81\xcf\x89'
Then, the byte string that is encoded in that base64 is in UTF-8; 然后,在该base64中编码的字节字符串为UTF-8; to get a Unicode string, you have to decode it:
要获取Unicode字符串,您必须对其进行解码:
>>> print base64.urlsafe_b64decode('Im3Osc6_z4HPgc-J==').decode('utf-8')
"mαορρω
With base64.decodestring
you got weird results because it just drops any character that is not part of the standard base64 alphabet, so it decoded incorrect bytes. 使用
base64.decodestring
您会得到奇怪的结果,因为它会丢弃不属于标准base64字母的任何字符,因此会解码不正确的字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.