Base64

Base64 is one of the two transfer encodings used in MIME e-mail messages to encode binary data entirely in characters of the 7-bit ASCII range so that it could be safely transmitted even through systems not supporting anything else (which was more of an issue in the 1970s when e-mail protocols were defined than at present). The other encoding is Quoted-printable, which is best suited for data that is mostly ASCII text but may have a few "unsafe" characters needing encoding. Base64 is best suited for binary data where all byte values from 0 to 255 are likely to occur, and encodes them efficiently (but not easily readably by the "naked eye").

A Base64-encoded message part is indicated in its MIME headers (following the Content-type header giving the type of data) with the line:

Content-Transfer-Encoding: Base64

The data is encoded as a sequence of base-64 digits, consisting of a character from a set of 64 characters, which (starting at the character representing zero) goes in the order:


 * Capital letters from A to Z
 * Lowercase letters from a to z
 * Digits from 0 to 9
 * The characters + and /

A base-64 digit (fitting in one byte of the encoded data) encodes six bits of the original data. Since a byte has eight bits, three bytes of the original file (24 bits) correspond to four base-64 digits. Thus, the encoding method requires you to consider each group of three bytes as a number (big-endian), and express it as four digits in the base-64 system. The padding character = is used to fill out a group of four characters if not needed to encode the end of the original data.

BinHex uses a similar encoding scheme (except for really early versions which used hexadecimal as implied by the name), but with a different set of 64 characters and some other format differences.

Specs

 * RFC 3548
 * RFC 4648

Software

 * GNU Coreutils → base64
 * Base64 online encoder/decoder
 * Base64 module for Perl

Other links

 * Content Transfer Encoding (Wikipedia)
 * Base64 (Wikipedia)