The Base64 encoding converts 8-bit characters into a universal character set,
which are represented identically in ASCII and EBCDIC. The
characters are indexed into a 64 printable character set table, and must be in
the following order:
Index Value
Encode Char
00
A
01
B
02
C
03
D
04
E
05
F
06
G
07
H
08
I
09
J
10
K
11
L
12
M
13
N
14
O
15
P
16
Q
Index Value
Encode Char
17
R
18
S
19
T
20
U
21
V
22
W
23
X
24
Y
25
Z
26
a
27
b
28
c
29
d
30
e
31
f
32
g
33
h
Index Value
Encode Char
34
i
35
j
36
k
37
l
38
m
39
n
40
o
41
p
42
q
43
r
44
s
45
t
46
u
47
v
48
w
49
x
50
y
Index Value
Encode Char
51
z
52
0
53
1
54
2
55
3
56
4
57
5
58
6
59
7
60
8
61
9
62
+
63
/
(PAD)
=
The procedure for encoding is as follows:
The data is subdivided into 3-byte groups forming a 24-bit stream.
Example . The 3-byte word "The" has the following 24-bit stream
(yellow).
Text
T
h
e
Byte Hex
0x54
0x68
0x65
Bit Stream
0
1
0
1
0
1
0
0
0
1
1
0
1
0
0
0
0
1
1
0
0
1
0
1
The 24-bit stream is further subdivided into 6 bits.
Example . The 24-bit stream for the word "The" is
subdivided as follows:
Text
T
h
e
Byte Hex
0x54
0x68
0x65
Bit Stream
0
1
0
1
0
1
0
0
0
1
1
0
1
0
0
0
0
1
1
0
0
1
0
1
6-Bit Group
0
1
0
1
0
1
0
0
0
1
1
0
1
0
0
0
0
1
1
0
0
1
0
1
6-Bit Value
21
6
33
37
The decimal value of each of the 6-bit stream is then mapped to
Base64 table for their corresponding encoded character.
Example . The 6-bit values are then mapped as follows to the
Base64 table:
The value 21 is mapped to "V".
The value 6 is mapped to "G".
The value 33 is mapped to "h".
The value 37 is mapped to "l".
Text
T
h
e
Byte Hex
0x54
0x68
0x65
Bit Stream
0
1
0
1
0
1
0
0
0
1
1
0
1
0
0
0
0
1
1
0
0
1
0
1
6-Bit Group
0
1
0
1
0
1
0
0
0
1
1
0
1
0
0
0
0
1
1
0
0
1
0
1
6-Bit Value
21
6
33
37
Encoding
V
G
h
l
The resulting encoding for the word "The" is "VGhl".
Because the data is subdivided in groups of 3 bytes, there are some cases where
the end of data has only 2 bytes or 1 byte left. These are
handled as follows:
One Byte . Add two NULL bytes at the end to
fulfill the 3 byte subgroup, and to accomplish adding zero bits to
the right. Translate the first two 6-bit value to the encoded
characters in the Base64 table, and append two "=" for padding.
Example . Assume there is only one character in the data -
"e". Two NULL bytes are added. The result of encoding "e" is "VQ==
" as shown below:
The value 21 is mapped to "V".
The value 16 is mapped to "Q".
Add two "=" to pad.
Text
e
NULL
NULL
Byte Hex
0x65
0x00
0x00
Bit Stream
0
1
1
0
0
1
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
6-Bit Group
0
1
0
1
0
1
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
6-Bit Value
21
16
0
0
Encoding
V
Q
=
=
Two Bytes . Add one NULL byte at the end to fulfill the
3 byte subgroup, and to accomplish adding zero bits to the right.
Translate the first three 6-bit value to the encoded characters in the Base64
table, and append one "=" for padding.
Example . Assume there is only two characters in the data -
"he". One NULL bytes are added. The result of encoding "he" is
"aGU= " as shown below:
The value 26 is mapped to "a".
The value 6 is mapped to "G".
The value 20 is mapped to "U".
Add one "= " to pad.
Text
h
e
NULL
Byte Hex
0x68
0x65
0x00
Bit Stream
0
1
1
0
1
0
0
0
0
1
1
0
0
1
0
1
0
0
0
0
0
0
0
0
6-Bit Group
0
1
1
0
1
0
0
0
0
1
1
0
0
1
0
1
0
0
0
0
0
0
0
0
6-Bit Value
26
6
20
0
Encoding
a
G
U
=
The following shows the encoding transformation of the string "The
car". The encoded result is "VGhlIGNhcg==". The actual encoded byte
stream is shown in the last row.