Base64
lastmod 2020-10-18created 2020-10-184 edits
|
In computing, Base 64 refers to an encoding scheme that converts 8-bit binary data (for example, executable files, ZIP files, etc.) into a sequence of characters made up only of characters from the common ASCII range, which is not affected by character encodings.
Taken literally, Base 64 means base-64 numeral system. Base 64 is particularly interesting in computing because 64 is a power of 2 (64 = 26), and it is the largest of the power-of-2 bases that can be represented using printable ASCII characters. That is, the next power, base 128, would need 128 symbols, but there are fewer than 128 printable ASCII characters.
For that reason this encoding is widely used for things like transferring binary data over e-mail. There are several variants of Base 64 depending on which characters and symbols are used, but all the well-known ones use the alphabet A-Z, a-z and 0-9 for the first 62 and differ only in which symbols are used for the last two.
MIME
The e-mail base64 defined by MIME is an encoding scheme that converts an arbitrary byte stream into printable ASCII characters. This encoding is defined as one of the content transfer encodings of MIME for transferring Internet e-mail.
The encoded string consists of 64 symbols: uppercase and lowercase letters, digits, and the "+" and "/" symbols, while "=" is used as a code marking the end.
The exact specification of base64 is defined in RFC 1421 and RFC 2045. It is defined to encode a sequence of 8-bit bytes, which matches the definition of a file on most systems. The output of base64 encoding is roughly 4/3 the size of the original, and usually takes the form of a string of seemingly meaningless characters.
The process of converting data to base64 is as follows. First, three bytes are put into a 24-bit buffer one byte at a time, starting from the top (MSB). If fewer than 3 bytes remain, the rest of the buffer is filled with 0. Then, starting from the top of the buffer, 6 bits1 at a time are cut off and their value read, and the character at that position among the 64 characters "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" arranged as follows is picked and output. If only one byte was input, only two of the output characters are used and the other two are padded with "="; if two bytes were input, only three of the output characters are used and the remaining one is padded with "=". This is to prevent bits that were not in the original from appearing when converting back to the original. Repeating this process until the input data ends completes the encoding.
For example, the following sentence, a Wikipedia slogan,
Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.
is converted to base64 as shown below through the process described above.
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0
aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1
c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0
aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdl
LCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=
For reference, the process of converting the first word, Man, is as follows.
| Text content | M | a | n | |||||||||||||||||||||
| ASCII | 77 | 97 | 110 | |||||||||||||||||||||
| Bit pattern | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 |
| Index | 19 | 22 | 5 | 46 | ||||||||||||||||||||
| Base64-Encoded | T | W | F | u | ||||||||||||||||||||
Libraries
See also
Favorite site
References
-
Because 64 = 26 ↩