Skip to content Skip to sidebar Skip to footer

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. Node does this correctly I believe. Could anyone help point out why python d

Solution 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.


Post a Comment for "Difference Between Python And Node Base64 Decoding"