A virtual teacher who reveals to you the great secrets of Base64

Python Base64 Decode

To decode Base64 to original data, Python provides the b64decode function of the base64 module. In addition, this module also provides the b64encode function, as well as other useful functions to decode Base64 strings using several algorithms specified in RFC 3548.

Usage:

  • b64decode(b64) - Discards all invalid characters from b64 and decodes it from Base64 to original data
  • b64decode(b64, altchars) - Replaces altchars with +/ from b64 and decodes it from Base64 to original data
  • b64decode(b64, altchars, validate) - If validate is set to True the b64 must contain only valid Base64 characters

Arguments:

  • b64 (required string) - The Base64 value that should be decoded
  • altchars (optional string; default None) - Two characters that should replace “+” and “/”
  • validate (optional bool; default False; added in Python 3) - Allows to decode only valid Base64 values

Return Values:

  • str - On success, it returns the original data

Supported Versions:

  • Python 2.4+

Changelog:

  • Python 3.1:
    • Adds the decodebytes function, which Base64 values to bytes
    • The function decodestring is deprecated
  • Python 3.3:
    • ASCII-only Unicode strings are now accepted by the decoding functions of the modern interface
  • Python 3.4:
    • Adds the a85decode function, which decodes Ascii85 to original data
    • Adds the b85decode function, which decodes Base85 to original data
    • Any bytes-like objects are now accepted by all decoding functions in the base64 module

Example #1 (print the result):

from base64 import b64decode
b64 = 'Z3VydQ=='
print(b64decode(b64)) #-> 'guru'

Example #2 (print unicode string on Python 3):

from base64 import b64decode
b64 = '4oKsMTAw'
print(b64decode(b64).decode('utf-8')) #-> '€100'

Example #3 (decode data from a custom Base64 alphabet):

from base64 import b64decode
b64 = 'PDw_Pz8-Pg=='
print(b64decode(b64)) #-> '<<<���'
print(b64decode(b64, '-_')) #-> '<<???>>'

Example #4 (validate characters on Python 3):

from base64 import b64decode
b64 = 'Z 3 V y d Q=='
print(b64decode(b64)) #-> b'guru'
print(b64decode(b64, None, True)) #-> binascii.Error: Non-base64 digit found

Example #5 (errors for incorrectly padded Base64 values):

# Python 2.7: TypeError: Incorrect padding
# Python 3.6: binascii.Error: Incorrect padding
b64decode('a=b')

Example #6 (TypeError on missing required argument):

# Python 2.7 - b64decode() takes at least 1 argument (0 given)
# Python 3.6 - b64decode() missing 1 required positional argument: 's'
b64decode()

Example #7 (TypeError on wrong argument type):

# Python 2.7 - a2b_base64() argument 1 must be string or buffer, not int
# Python 3.6 - argument should be a bytes-like object or ASCII string, not 'int'
b64decode(123)
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.

Loading...