问题
I am puzzled at this base64 decoding issue, and it seems that python and node.js does this differently. Node does this correctly I believe. Could anyone help point out why python does not work here?
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
回答1:
What you provided is actually not a standard base64, but a URL-safe base64
which substitutes
-
instead of+
and_
instead of/
in the standard Base64 alphabet"
To decode it in Python you need to use 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; to get a Unicode string, you have to decode it:
>>> 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.
来源:https://stackoverflow.com/questions/49885049/difference-between-python-and-node-base64-decoding