当前位置:   article > 正文

python_base64_binascii.Error: Incorrect padding解决

binascii.error: incorrect padding

问题描述:

在使用b64decode对加密后的文件进行解密的时候报错,如下:

  1. Traceback (most recent call last):
  2. File "E:/project/allow/zt/xx01_xyz.py", line 13, in <module>
  3. result = b64decode("aHR0cCUzQSUyRiUyRnZpZGVvLnlqZjEzOC5jb20lM0E4MDkxJTJGMjAxODEyMzElMkYyd3prQjRGbSUyRmluZGV4Lm0zdTg")
  4. File "E:\Anaconda3\lib\base64.py", line 87, in b64decode
  5. return binascii.a2b_base64(s)
  6. binascii.Error: Incorrect padding

Incorrect padding:填充不正确

以下为base64.py文件中方法定义

  1. def b64decode(s, altchars=None, validate=False):
  2. """Decode the Base64 encoded bytes-like object or ASCII string s.
  3. Optional altchars must be a bytes-like object or ASCII string of length 2
  4. which specifies the alternative alphabet used instead of the '+' and '/'
  5. characters.
  6. The result is returned as a bytes object. A binascii.Error is raised if
  7. s is incorrectly padded.
  8. If validate is False (the default), characters that are neither in the
  9. normal base-64 alphabet nor the alternative alphabet are discarded prior
  10. to the padding check. If validate is True, these non-alphabet characters
  11. in the input result in a binascii.Error.
  12. """
  13. s = _bytes_from_decode_data(s)
  14. if altchars is not None:
  15. altchars = _bytes_from_decode_data(altchars)
  16. assert len(altchars) == 2, repr(altchars)
  17. s = s.translate(bytes.maketrans(altchars, b'+/'))
  18. if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s):
  19. raise binascii.Error('Non-base64 digit found')
  20. return binascii.a2b_base64(s)

原文:    

Optional altchars must be a bytes-like object or ASCII string of length 2 which specifies the alternative alphabet used instead of the '+' and '/' characters.

翻译:

可选altchars必须是长度为2的对象或ASCII字符串之类的字节它指定使用的替代字母表,而不是“+”和“/”字符。

原因分析:

传入的参数的长度不是2的对象,在参数最后加上等于号"="(一个或者两个)

修改后代码:

  1. result = b64decode("aHR0cCUzQSUyRiUyRnZpZGVvLnlqZjEzOC5jb20lM0E4MDkxJTJGMjAxODEyMzElMkYyd3prQjRGbSUyRmluZGV4Lm0zdTg=")
  2. print(type(result))
  3. print(result)

运行成功!!!

执行结果:

<class 'bytes'>
b'http%3A%2F%2Fvideo.yjf138.com%3A8091%2F20181231%2F2wzkB4Fm%2Findex.m3u8'

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/335662
推荐阅读
相关标签
  

闽ICP备14008679号