Python Base64
base64 is a module that supports en-/decoding data with Base16, Base32, Base64, and Base85 encodings.
Usage
Throughout this document, a bytes-like object is any of bytes, bytearray, array.array, or a strictly ASCII str.
To go between an base64-encoded bytes-like object and bytes object, try:
import base64 encoded = base64.b64encode(b'data to be encoded') # b'ZGF0YSB0byBiZSBlbmNvZGVk' data = base64.b64decode(encoded) # b'data to be encoded'
Ascii85
Use a85encode() and a85decode() to go between an Ascii85-encoded bytes-like object and bytes.
Base16
Use b16encode() and b16decode() to go between an base16-encoded bytes-like object and bytes.
A binascii.Error is raised by b16decode() if the bytes-like object is incorrectly padded or includes characters that are invalid for base16-encoding.
Base32
For the conventional (RFC 3548, RFC 4648) base32 alphabet (A-Z, 2-7), use b32encode() and b32decode() to go between an base32-encoded bytes-like object and bytes.
For the alternative 'base32hex' (RFC 2938, RFC 4648) alphabet (0-9, A-V) use b32hexencode() and b32hexdecode().
A binascii.Error is raised by b32decode and b32hexdecode if the bytes-like object is incorrectly padded or includes characters that are invalid for base32-encoding.
Base64
See the example above.
A binascii.Error is raised by b64decode if the bytes-like object is incorrectly padded for base64-encoding. If the keyword-only argument validate is set to True, a binascii.Error is raised by b64decode if the bytes-like object includes characters that are invalid for base64-encoding.
Alternative Characters
The keyword-only argument altchars allows for alternative characters to be used in place of + and /. This is primarily useful for embedding data into URLs, as those characters are not allowable in a URL component. A ValueError is raised if altchars is set to anything other than a two-long bytes-like object. Consider instead using one of:
standard_b64encode and standard_b64decode disallows this functionality
urlsafe_b64encode and urlsafe_b64decode hardcodes the alternative characters of - and _
Base85
Use b85encode() and b85decode() to go between an base85-encoded bytes-like object and bytes.
See also
Python base64 module documentation
Python Module of the Day article for base64