Tag Archives: Decoding error

[Solved] Error “incorrect padding” in decoding of Base64 module in Python

Problem description
decodes the field information encoded by Base64, which is normally decoded in the base64 encoding and decoding tool, but b64decode and A2B in the modules Base64 and binascii under python_ Decoding error in Base64 and other methods
the error information is as follows

---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
<ipython-input-11-787bc11958b4> in get_proxies(urls)
     14         try:
---> 15             raw = base64.b64decode(response)
     16         except Exception as r:

c:\program files\python3\lib\base64.py in b64decode(s, altchars, validate)
     86         raise binascii.Error('Non-base64 digit found')
---> 87     return binascii.a2b_base64(s)
     88 

Error: Incorrect padding

Solution

Base64 in Python is read in 4, so the field to be decoded should be a multiple of 4, not enough to add ‘=’

# The field a to be decoded is judged, and if it is a multiple of 4, it is unchanged, and if not, how much is missing, how much is made up
a = a + '=' * (4 - len(a) % 4) if len(a) % 4 != 0 else a