
Privacy-enhanced mail: The first known standardized use of the encoding now called MIME Base64 was in the Privacy-enhanced Electronic Mail (PEM) protocol, proposed by RFC 989 in 1987. This notably concerns the last two characters used in the index table for index 62 and 63, and the character used for padding (which may be mandatory in some protocols, or removed in others). Implementations may have some constraints on the alphabet used for representing some bit patterns. A single remaining encoded character is not possible (because a single base 64 character only contains 6 bits, and 8 bits are required to create a byte, so a minimum of 2 base 64 characters are required : the first character contributes 6 bits, and the second character contributes its first 2 bits). In this situation only two or three characters shall remain. A single '=' indicates that the four characters will decode to only two bytes, while '=' indicates that the four characters will decode to only a single byte.ĭecoding Base64 without padding: Without padding, after normal decoding of four characters to three bytes over and over again, fewer than four encoded characters may remain. The only exceptions are when padding characters exist. One case in which padding characters are required is concatenating multiple Base64 encoded files.ĭecoding Base64 with padding: When decoding Base64 text, four characters are typically converted back to three bytes.

In some implementations, the padding character is mandatory, while for others it is not used. In theory, the padding character is not needed for decoding, since the number of missing bytes can be calculated from the number of Base64 digits.

Specifically, given an input of n bytes, the output will be 4 bytes long, including padding characters. The ratio of output bytes to input bytes is 4:3 (33% overhead). The same characters will be encoded differently depending on their position within the three-octet group which is encoded to produce the four characters.

Output padding: The final '=' sequence indicates that the last group contained only one byte, and '=' indicates that it contained two bytes. If there is only one significant input octet (e.g., 'M'), or when the last input group contains only one octet, all 8 bits will be captured in the first two Base64 digits (12 bits) the four least significant bits of the last content-bearing 6-bit block will turn out to be zero, and discarded on decoding (along with the following '=' padding characters) If there are only two significant input octets (e.g., 'Ma'), or when the last input group contains only two octets, all 16 bits will be captured in the first three Base64 digits (18 bits) the two least significant bits of the last content-bearing 6-bit block will turn out to be zero, and discarded on decoding (along with the following '=' padding characters) '=' padding characters might be added to make the last encoded block contain four Base64 characters. Groups of 6 bits (6 bits have a maximum of 2 6 = 64 different binary values) are converted into individual numbers from left to right, which are then converted into their corresponding Base64 character values. For instance, uuencode uses uppercase letters, digits, and many punctuation characters, but no lowercase. The earliest instances of this type of encoding were created for dialup communication between systems running the same OS - e.g., uuencode for UNIX, BinHex for the TRS-80 (later adapted for the Macintosh) - and could therefore make more assumptions about what characters were safe to use. Other variations share this property but differ in the symbols chosen for the last two values an example is UTF-7. For example, MIME's Base64 implementation uses A–Z, a–z, and 0–9 for the first 62 values. This combination leaves the data unlikely to be modified in transit through information systems, such as email, that were traditionally not 8-bit clean. The general strategy is to choose 64 characters that are both members of a subset common to most encodings, and also printable. The particular set of 64 characters chosen to represent the 64 place-values for the base varies between implementations. Three 8-bit bytes (i.e., a total of 24 bits) can therefore be represented by four 6-bit base64 digits.

The term Base64 originates from a specific MIME content transfer encoding.Įach base64 digit represents exactly 6 bits of data. Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation.
