The following is an excerpt from section 6.8 (Base64 Content-Transfer-Encoding) of RFC 2045. It gives details of Base64 encoding that is used for encoding of binary attchments in email. The encoding process represents 24-bit groups of input bits as output strings of 4 encoded characters. Proceeding from left to right, a 24-bit input group is formed by concatenating 3 8bit input groups. These 24 bits are then treated as 4 concatenated 6-bit groups, each of which is translated into a single digit in the base64 alphabet. When encoding a bit stream via the base64 encoding, the bit stream must be presumed to be ordered with the most-significant-bit first. That is, the first bit in the stream will be the high-order bit in the first 8bit byte, and the eighth bit will be the low-order bit in the first 8bit byte, and so on. Each 6-bit group is used as an index into an array of 64 printable characters. The character referenced by the index is placed in the output string. These characters are identified in table below. Value Encoding Value Encoding Value Encoding Value Encoding 0A 26a 1B 27b 2C 28c .. ... .. ... 24 Y 50y 25 Z 51z 520 62+ 531 63/ 542 .. . (pad) = ... 608 619 The encoded output stream must be represented in lines of no more than 76 characters each. All line breaks or other characters not found in the table must be ignored by decoding software. Special processing is performed if fewer than 24 bits are available at the end of the data being encoded. A full encoding quantum is always completed at the end of a body. When fewer than 24 input bits are available in an input group, zero bits are added (on the right) to form an integral number of 6-bit groups. Padding at the end of the data is performed using the ‘=’ character. Since all base64 input is an integral number of octets, only the following cases can arise:
2/2 Input There are multiple datasets, each terminated by the ‘#’ mark. The last dataset, which should not be processed, contains no character other than the terminating ‘#’ mark. Each dataset consists of a valid Base64 encoding of some (possibly binary) data. Output Output decoded data for each dataset followed immediately by the ‘#’ mark. Sample Input VGhpc0lzVGVzdA==
QSBUZXN0IElucHV0W3so KX1d
Sample Output ThisIsTest#A Test Input[{()}]#