Base64 Encoding

The base64encode function allows you to encode data in the MIME Base64 format. On the other hand, the base64decode function is used to decode the MIME Base64-encoded data. Let’s go through each of these functions in detail. Here Base64 encoding is used for the mappings field. The comma and semicolon delimited snippets are the Base64 encoded binary data of integers encoded as variable-length quantities (VLQ). Images and source maps are just a couple places Base64 encoding is used.

Source code:Lib/base64.py

This module provides functions for encoding binary data to printableASCII characters and decoding such encodings back to binary data.It provides encoding and decoding functions for the encodings specified inRFC 3548, which defines the Base16, Base32, and Base64 algorithms,and for the de-facto standard Ascii85 and Base85 encodings.

The RFC 3548 encodings are suitable for encoding binary data so that it cansafely sent by email, used as parts of URLs, or included as part of an HTTPPOST request. The encoding algorithm is not the same as theuuencode program.

There are two interfaces provided by this module. The modern interfacesupports encoding bytes-like objects to ASCIIbytes, and decoding bytes-like objects orstrings containing ASCII to bytes. Both base-64 alphabetsdefined in RFC 3548 (normal, and URL- and filesystem-safe) are supported.

The legacy interface does not support decoding from strings, but it doesprovide functions for encoding and decoding to and from file objects. It only supports the Base64 standard alphabet, and it addsnewlines every 76 characters as per RFC 2045. Note that if you are lookingfor RFC 2045 support you probably want to be looking at the emailpackage instead.

Changed in version 3.3: ASCII-only Unicode strings are now accepted by the decoding functions ofthe modern interface.

Changed in version 3.4: Any bytes-like objects are now accepted by allencoding and decoding functions in this module. Ascii85/Base85 support added.

The modern interface provides:

base64.b64encode(s, altchars=None)

Encode the bytes-like objects using Base64 and return the encodedbytes.

Optional altchars must be a bytes-like object of at leastlength 2 (additional characters are ignored) which specifies an alternativealphabet for the + and / characters. This allows an application to e.g.generate URL or filesystem safe Base64 strings. The default is None, forwhich the standard Base64 alphabet is used.

base64.b64decode(s, altchars=None, validate=False)

Decode the Base64 encoded bytes-like object or ASCII strings and return the decoded bytes.

Optional altchars must be a bytes-like object or ASCII string ofat least length 2 (additional characters are ignored) which specifies thealternative alphabet used instead of the + and / characters.

A binascii.Error exception is raisedif s is incorrectly padded.

If validate is False (the default), characters that are neitherin the normal base-64 alphabet nor the alternative alphabet arediscarded prior to the padding check. If validate is True,these non-alphabet characters in the input result in abinascii.Error.

base64.standard_b64encode(s)

Encode bytes-like objects using the standard Base64 alphabetand return the encoded bytes.

base64.standard_b64decode(s)

Decode bytes-like object or ASCII string s using the standardBase64 alphabet and return the decoded bytes.

base64.urlsafe_b64encode(s)

Base64 Encoding Explained

Encode bytes-like objects using theURL- and filesystem-safe alphabet, whichsubstitutes - instead of + and _ instead of / in thestandard Base64 alphabet, and return the encoded bytes. The resultcan still contain =.

base64.urlsafe_b64decode(s)

Decode bytes-like object or ASCII string susing the URL- and filesystem-safealphabet, which substitutes - instead of + and _ instead of/ in the standard Base64 alphabet, and return the decodedbytes.

base64.b32encode(s)

Encode the bytes-like objects using Base32 and return theencoded bytes.

base64.b32decode(s, casefold=False, map01=None)

Decode the Base32 encoded bytes-like object or ASCII string s andreturn the decoded bytes.

Optional casefold is a flag specifyingwhether a lowercase alphabet is acceptable as input. For security purposes,the default is False.

RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O(oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)or letter L (el). The optional argument map01 when not None, specifieswhich letter the digit 1 should be mapped to (when map01 is not None, thedigit 0 is always mapped to the letter O). For security purposes the default isNone, so that 0 and 1 are not allowed in the input.

A binascii.Error is raised if s isincorrectly padded or if there are non-alphabet characters present in theinput.

base64.b16encode(s)

Encode the bytes-like objects using Base16 and return theencoded bytes.

base64.b16decode(s, casefold=False)

Decode the Base16 encoded bytes-like object or ASCII string s andreturn the decoded bytes.

Optional casefold is a flag specifying whether alowercase alphabet is acceptable as input. For security purposes, the defaultis False.

A binascii.Error is raised if s isincorrectly padded or if there are non-alphabet characters present in theinput.

base64.a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False)

Encode the bytes-like objectb using Ascii85 and return theencoded bytes.

foldspaces is an optional flag that uses the special short sequence ‘y’instead of 4 consecutive spaces (ASCII 0x20) as supported by ‘btoa’. Thisfeature is not supported by the “standard” Ascii85 encoding.

wrapcol controls whether the output should have newline (b'n')characters added to it. If this is non-zero, each output line will beat most this many characters long.

pad controls whether the input is padded to a multiple of 4before encoding. Note that the btoa implementation always pads.

adobe controls whether the encoded byte sequence is framed with <~and ~>, which is used by the Adobe implementation.

base64.a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' tnrv')

Decode the Ascii85 encoded bytes-like object or ASCII string b andreturn the decoded bytes.

Base64 Encoding

foldspaces is a flag that specifies whether the ‘y’ short sequenceshould be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).This feature is not supported by the “standard” Ascii85 encoding.

adobe controls whether the input sequence is in Adobe Ascii85 format(i.e. is framed with <~ and ~>).

Base64 Encoding Tool

ignorechars should be a bytes-like object or ASCII stringcontaining characters to ignorefrom the input. This should only contain whitespace characters, and bydefault contains all whitespace characters in ASCII.

Base64 Encode Username And Password

New in version 3.4.

base64.b85encode(b, pad=False)

Encode the bytes-like objectb using base85 (as used in e.g.git-style binary diffs) and return the encoded bytes.

Base64 Encoding Api

If pad is true, the input is padded with b'0' so its length is amultiple of 4 bytes before encoding.

base64.b85decode(b)

Decode the base85-encoded bytes-like object or ASCII string b andreturn the decoded bytes. Padding is implicitly removed, ifnecessary.

New in version 3.4.

The legacy interface:

base64.decode(input, output)

Decode the contents of the binary input file and write the resulting binarydata to the output file. input and output must be file objects. input will be read until input.readline() returns anempty bytes object.

base64.decodebytes(s)

Decode the bytes-like objects, which must contain one or morelines of base64 encoded data, and return the decoded bytes.

base64.encode(input, output)

Encode the contents of the binary input file and write the resulting base64encoded data to the output file. input and output must be fileobjects. input will be read until input.read() returnsan empty bytes object. encode() inserts a newline character (b'n')after every 76 bytes of the output, as well as ensuring that the outputalways ends with a newline, as per RFC 2045 (MIME).

Base64 Encoding Js

base64.encodebytes(s)

Base64 Encoding Url

Encode the bytes-like objects, which can contain arbitrary binarydata, and return bytes containing the base64-encoded data, with newlines(b'n') inserted after every 76 bytes of output, and ensuring thatthere is a trailing newline, as per RFC 2045 (MIME).

New in version 3.1.

An example usage of the module: Mymyry.

Base64 Encoding

Base64 Encoding Python

See also

Module binascii

Support module containing ASCII-to-binary and binary-to-ASCII conversions.

RFC 1521 - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies

Section 5.2, “Base64 Content-Transfer-Encoding,” provides the definition of thebase64 encoding.