python3 base64.b64decode Base64解码报错: Incorrect padding

Star英

关注

阅读 102

2022-07-12


根据Base64加密的原理,base64编码后的字符长度为4的倍数,如果不足4位,用​​=​​​来补位。如果没有补位,就会报错:​​Incorrect padding​​​。解决方法就是把缺少的​​=​​补上。

import base64

def base64_decode(encode):
"""
解决base64编码结尾缺少=报错的问题
"""
missing_padding = 4 - len(encode) % 4
if missing_padding:
encode += '=' * missing_padding
decode = base64.b64decode(encode)
return decode

print(base64_decode("aGVsbG8="))
print(base64_decode("aGVsbG8"))

print(base64.b64decode("aGVsbG8"))

python3 base64.b64decode Base64解码报错: Incorrect padding_解决方法


精彩评论(0)

0 0 举报