difference between python and node base64 decoding

末鹿安然 提交于 2019-12-25 01:32:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!