Base64 is one of the most common encoding methods used to transmit 8-bit code on the network, base64 requires that each three 8-bit bytes be converted into four 6-bit bytes (3*8 = 4*6 = 24), and then 6-bit bytes be added with two more high 0 values, it consists of four 8-bit bytes. That is to say, the converted string is theoretically 1/3 longer than the original one. After conversion, a base64 code table can be used to obtain base64 encoding. If the character string is not a multiple of 3, '=' is used in the case of insufficient bytes during conversion.
Base64 code table:
Index |
Character |
Index |
Character |
Index |
Character |
Index |
Character |
0 |
A |
17 |
R |
34 |
I |
51 |
Z |
1 |
B |
18 |
S |
35 |
J |
52 |
0 |
2 |
C |
19 |
T |
36 |
K |
53 |
1 |
3 |
D |
20 |
U |
37 |
L |
54 |
2 |
4 |
E |
21 |
V |
38 |
M |
55 |
3 |
5 |
F |
22 |
W |
39 |
N |
56 |
4 |
6 |
G |
23 |
X |
40 |
O |
57 |
5 |
7 |
H |
24 |
Y |
41 |
P |
58 |
6 |
8 |
I |
25 |
Z |
42 |
Q |
59 |
7 |
9 |
J |
26 |
A |
43 |
R |
60 |
8 |
10 |
K |
27 |
B |
44 |
S |
61 |
9 |
11 |
L |
28 |
C |
45 |
T |
62 |
+ |
12 |
M |
29 |
D |
46 |
U |
63 |
/ |
13 |
N |
30 |
E |
47 |
V |
|
|
14 |
O |
31 |
F |
48 |
W |
|
|
15 |
P |
32 |
G |
49 |
X |
|
|
16 |
Q |
33 |
H |
50 |
Y |
|
|
For example, if the source string is
00110110
11001100
11011010, it is divided into 4 bytes according to each 6 bits, and in the high 2 is zero (6 bits are insufficient, so in the high 2 bits are zero, constitute one byte), the result is as follows:
00001101
00101100
00110011
00011010
The corresponding decimal values are:
13
44
51
26
According to the code table, it can be base64 encoded as nsza. Decoding is only the inverse process of encoding.
The base64 encoding implemented by VC is as follows:
// Define a code table
Const char * jbase64: pszencodetable = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /";
// Decoding function
Bool jbase64: encode (<br/> const byte * pbytein, <br/> int ninlength, <br/> byte * pbyteout, <br/> Int & noutlength <br/>) <br/> {<br/> bool breturn = false; <br/> assert (pbytein! = NULL); <br/> assert (pbyteout! = NULL); <br/> assert (ninlength> 0); <br/> If (pbytein = NULL | pbyteout = NULL | ninlength <= 0) <br/>{< br/> goto exit0; <br/>}</P> <p> int ncount = ninlength/3; <br/> int nmode = ninlength % 3; <br/> byte * pout = pbyteout; <br/> for (INT I = 0; I <ncount; I ++) <br/>{< br/> * pout ++ = pszencodetable [(pbytein [0]> 2)]; <br/> * pout ++ = pszencodetable [(pbytein [0] & 3) <4) | (pbytein [1]> 4)]; <br/> * pout ++ = pszencodetable [(pbytein [1] & 0xf) <2 | (pbytein [2]> 6)]; <br/> * pout ++ = pszencodetable [(pbytein [2] & 0x3f)]; <br/> pbytein + = 3; <br/>}< br/> If (nmode! = 0) <br/>{< br/> * pout ++ = pszencodetable [(pbytein [0]> 2)]; <br/> * pout ++ = pszencodetable [(pbytein [0] & 3) <4) | (nmode = 1 )? 0: (pbytein [1]> 4)]; <br/> * pout ++ = (nmode = 1 )? '=': Pszencodetable [(pbytein [1] & 0xf) <2)]; <br/> * pout ++ = '; <br/>}< br/> * pout = '/0'; <br/> noutlength = static_cast <int> (pout-pbyteout ); <br/> breturn = true; <br/> exit0: <br/> return breturn; <br/>}
// Define the decoding table
Const unsigned char jbase64: pszdecodetable [256] =
{
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255, 62,255,255,255, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,
255,254,255,255,255, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255
};
// Decoding function
Bool jbase64: Decode (<br/> const byte * pbytein, <br/> int ninlength, <br/> byte * pbyteout, <br/> Int & noutlength <br/>) <br/> {<br/> bool breturn = false; <br/> assert (pbytein! = NULL); <br/> assert (pbyteout! = NULL); <br/> assert (ninlength> 0); <br/> assert (ninlength % 4 = 0 ); <br/> If (pbytein = NULL | pbyteout = NULL | ninlength % 4! = 0) <br/>{< br/> goto exit0; <br/>}< br/> byte TMP [4]; <br/> byte * pout = pbyteout; <br/> int nlength = ninlength/4; <br/> for (INT I = 0; I <nlength; I ++) <br/> {<br/> TMP [0] = pszdecodetable [pbytein [0]; <br/> TMP [1] = pszdecodetable [pbytein [1]; <br/> TMP [2] = pszdecodetable [pbytein [2]; <br/> TMP [3] = pszdecodetable [pbytein [3]; </P> <p> * pout ++ = (TMP [0] <2) | (TMP [1]> 4); <br/> * Po Ut ++ = (TMP [1] <4) | (TMP [2] = 254? 0: TMP [2]> 2); <br/> * pout ++ = (TMP [2] = 254? 0: TMP [2] <6) | (TMP [3] = 254? 0: TMP [3]); <br/> pbytein + = 4; <br/>}< br/> * pout = '/0 '; <br/> noutlength = static_cast <int> (pout-pbyteout); <br/> breturn = true; <br/> exit0: <br/> return breturn; <br/>}