Base64 encoding is used whenever a file needs to be sent via the Internet mail system and the 7bit and quoted-printable encoding types are unsuitable. In other words, any form of data can be base64 encoded (since octets of arbitrary sequence are allowed), but it is more efficient to leave 7-bit text alone and to encode other textual data with quoted-printable. Base64 encoding is described in RFC 2045.

Base64-encoded information is not meant to be human readable, even if the original data was.

Base64 encoding takes three octets (24 bits) and maps them into four 6-bit blocks, then represents each 6-bit block with a character in a 64-character alphabet (2 to the 6th power is 64). Because of this mapping, base64 encoded information is about one-third larger than the original data.

To encode a bit stream with the base64 method, first convert the bit stream into an octet stream, if necessary. As described in the section “Quoted-Printable,” earlier in this chapter, an octet is a sequence of 8 bits in the network-standard big-endian order.

Next, read the first 24 bits and convert them into four 6-bit blocks. Represent each block as a letter from the following alphabet. Use the decimal value of the block as an index into the alphabet table.

Value Encoding Value Encoding Value Encoding Value Encoding 0 A 16 Q 32 g 48 w 1 B 17 R 33 h 49 x 2 C 18 S 34 i 50 y 3 D 19 T 35 j 51 z 4 E 20 U 36 k 52 0 5 F 21 V 37 1 53 1 6 G 22 W 38 m 54 2 7 H 23 X 39 n 55 3 8 I 24 y 40 o 56 4 9 J 25 Z 41 p 57 5 ...

Get Programming Internet Email now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.