The term “Base64” refers to a specific MIME content-transfer encoding. It’s also used as a generic term for any similar encoding scheme that encodes binary data by treating it numerically and translating it into a base-64 representation. The particular choice of base is due to the history of character set encoding; one can choose a set of 64 characters that is both part of the subset common to most encodings, and also printable. This combination leaves the data unlikely to be modified in transit through systems, such as email, which were not intended for handling binary data.
Base64 to the Rescue
Base64 encoding uses three eight-bit bytes that represent four printable ASCII characters. The encoding requires two steps.
The first step converts three bytes to four numbers of six bits. Each character in the ASCII standard consists of seven bits. Base64 uses only six bits (corresponding to 2^6 = 64 characters) to ensure the encoded data is both printable and humanly readable?the process doesn’t use any of the special characters available in ASCII. These 64 characters (hence the name Base64) include 10 digits, 26 lowercase characters, 26 uppercase characters, and the plus (+) and forward slash (/) symbols.
For example, if you start with the three bytes 155, 162, and 233, the corresponding (and intimidating) bit stream is 100110111010001011101001, which in turn corresponds to the 6-bit values 38, 58, 11, and 41.
The second step converts these numbers to ASCII characters using the Base64 encoding table. In the example, the four 6-bit numbers translate to the ASCII sequence “m6Lp”.
155 -> 10011011 162 -> 10100010 233 -> 11101001 100110 -> 38 111010 -> 58 001011 -> 11 101001 -> 41 38 -> m 58 -> 6 11 -> L 41 -> p
This two-step process gets applied to the entire sequence of bytes to be encoded. To ensure that the encoded data can be printed properly, and does not exceed any mail server’s line-length limit, the conversion process inserts newline characters to keep line lengths below 76 characters. The process encodes the newline characters just like all the other data.