pythonbase64decodeincorrectpadding错误解决方法

python的base64.decodestring方法做base64解码时报错:

代码如下:

traceback (most recent call last):
file “/export/www/outofmemory.cn/controllers/user.py”, line 136, in decryptpassword
encryptpwd = base64.b64decode(encryptpwd)
file “/usr/lib/python2.7/base64.py”, line 76, in b64decode
raise typeerror(msg)
typeerror: incorrect padding

这也算是python的一个坑吧,解决此问题的方法很简单,对base64解码的string补齐等号就可以了,如下代码:

代码如下:

def decode_base64(data):
“””decode base64, padding being optional.

:param data: base64 data as an ascii byte string
:returns: the decoded byte string.

“””
missing_padding = 4 – len(data) % 4
if missing_padding:
data += b’=’* missing_padding
return base64.decodestring(data)

Posted in 未分类

发表评论