Base64 Encoding

From Vectivus
Jump to: navigation, search

Overview

Base64 encoding is used to transmit binary data when only ASCII data can be sent (although it is often used to mask data as well). Encoding into base64 requires using four bytes for every three bytes of data, so while it simplifies the types of data that must be handled (down to just 64 characters), it is inefficient in doing so. Base64 encoded data does compress well, however.

Encoding

Encoding is accomplished by taking the three bytes (24 bits) and writing them out, then breaking those into four sets of six bits. Each of those four sets is then looked up against a predefined table (see below) to determine the encoding. For example, using the common string:

Hello world

Would result in the following binary:

01001000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100

That then gets split into sets of six bits (and padded with zeroes):

010010 000110 010101 101100 011011 000110 111100 100000 011101 110110 111101 110010 011011 000110 010000 000000

Or in decimal:

18 6 21 44 27 6 60 32 29 54 61 50 27 6 16 0

And following that it's a simple lookup to get the final answer:

SGVsbG8gd29ybGQ=

Decoding

Decoding simply reverses the process. It takes each encoded character and reverses it into the six bit value in the lookup table, then after four such conversions splits the 24 bits back into three single bytes.

Characteristics

Aside from being able to identify base64 encoding by the consistent character set (A-Za-z0-9+/), it is often also identifiable by the presence of one or two "=" characters at the end of the string but not anywhere else, since the "=" character is padding used to specify all zeroes (no character present).

Base64 Lookup Table

Value Char   Value Char   Value Char   Value Char
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 l 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
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /