一、编码规则
Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码。它将需要编码的数据拆分成字节数组。以3个字节为一组。按顺序排列24 位数据,再把这24位数据分成4组,即每组6位。再在每组的的最高位前补两个0凑足一个字节。这样就把一个3字节为一组的数据重新编码成了4个字节。当所要编码的数据的字节数不是3的整倍数,也就是说在分组时最后一组不够3个字节。这时在最后一组填充1到2个0字节。并在最后编码完成后在结尾添加1到2个 “=”。
以下为Base64编码表:
码值 | 字符 | 码值 | 字符 | 码值 | 字符 | 码值 | 字符 |
---|---|---|---|---|---|---|---|
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 | / |
例如:将对ABC进行Base64编码:
1、首先取ABC对应的ASCII码值。A(65)B(66)C(67);
2、再取二进制值A(01000001)B(01000010)C(01000011);
3、然后把这三个字节的二进制码接起来(010000010100001001000011);
4、 再以6位为单位分成4个数据块,并在最高位填充两个0后形成4个字节的编码后的值,(00010000)(00010100)(00001001)(00000011),其中蓝色部分为真实数据;
5、再把这四个字节数据转化成10进制数得(16)(20)(9)(3);
6、最后根据BASE64给出的64个基本字符表,查出对应的ASCII码字符(Q)(U)(J)(D),这里的值实际就是数据在字符表中的索引。
以下为实验代码:
char *inBase64Encode( const unsigned char * inBindata, char * inBase64, int inBinlength )
{
int i, j;
unsigned char lCurrent;
for ( i = 0, j = 0 ; i < inBinlength ; i += 3 )
{
lCurrent = (inBindata[i] >> 2) ;
lCurrent &= (unsigned char)0x3F;
inBase64[j++] = inBase64char[(int)lCurrent];
lCurrent = ( (unsigned char)(inBindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;
if ( i + 1 >= inBinlength )
{
inBase64[j++] = inBase64char[(int)lCurrent];
inBase64[j++] = '=';
inBase64[j++] = '=';
break;
}
lCurrent |= ( (unsigned char)(inBindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );
inBase64[j++] = inBase64char[(int)lCurrent];
lCurrent = ( (unsigned char)(inBindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;
if ( i + 2 >= inBinlength )
{
inBase64[j++] = inBase64char[(int)lCurrent];
inBase64[j++] = '=';
break;
}
lCurrent |= ( (unsigned char)(inBindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );
inBase64[j++] = inBase64char[(int)lCurrent];
lCurrent = ( (unsigned char)inBindata[i+2] ) & ( (unsigned char)0x3F ) ;
inBase64[j++] = inBase64char[(int)lCurrent];
}
inBase64[j] = '\0';
return 0;
}