您的位置:首页 > 其它

BASE64编码和解码

2009-12-21 12:00 295 查看
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3,转换之后通过一个BASE64的码表就可以得到BASE64编码。如果字符串不是3的倍数的话,转换时字节不足的地方用‘=’代替。

BASE64码表:

索引
对应字符
索引
对应字符
索引
对应字符
索引
对应字符
0A
17R
34i
51z
1B
18S
35j
520
2C
19T
36k
531
3D
20U
37l
542
4E
21V
38m
553
5F
22W
39n
564
6G
23X
40o
575
7H
24Y
41p
586
8I
25Z
42q
597
9J
26a
43r
608
10K
27b
44s
619
11L
28c
45t
62+
12M
29d
46u
63/
13N
30e
47v
14O
31f
48w
15P
32g
49x
16Q
33h
50y
举个例子:假如源字符串是

00110110

11001100

11011010,则按照每6位划分成4个字节,并在高2为补零(6位不足,所以在高2位补零,组成一字节),结果如下:

00001101

00101100

00110011

00011010

对应的十进制分别是:

13

44

51

26

根据码表,可以BASE64编码为:Nsza。解码只是编码的逆过程。

vc实现BASE64编码如下:

//定义码表

const char* JBase64::pszEncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

//解码函数

bool JBase64::Encode(
const BYTE* pByteIn,
int nInLength,
BYTE* pByteOut,
int& nOutLength
)
{
bool bReturn = false;
assert(pByteIn != NULL);
assert(pByteOut != NULL);
assert(nInLength > 0);
if (pByteIn == NULL || pByteOut == NULL || nInLength <= 0)
{
goto exit0;
}

int nCount  = nInLength / 3;
int nMode   = nInLength % 3;
BYTE* pOut  = pByteOut;
for (int i = 0; i < nCount; i++)
{
*pOut++ = pszEncodeTable[(pByteIn[0] >> 2)];
*pOut++ = pszEncodeTable[((pByteIn[0] & 3) << 4) | (pByteIn[1] >> 4)];
*pOut++ = pszEncodeTable[(pByteIn[1] & 0xf) << 2 | (pByteIn[2] >> 6)];
*pOut++ = pszEncodeTable[(pByteIn[2] & 0x3f)];
pByteIn += 3;
}
if (nMode != 0)
{
*pOut++ = pszEncodeTable[(pByteIn[0] >> 2)];
*pOut++ = pszEncodeTable[((pByteIn[0] & 3) << 4) | ((nMode == 1) ? 0 : (pByteIn[1] >> 4))];
*pOut++ = (nMode == 1) ? '=':pszEncodeTable[((pByteIn[1] & 0xf) << 2)];
*pOut++ = '=';
}
*pOut = '/0';
nOutLength = static_cast<int>(pOut - pByteOut);
bReturn = true;
exit0:
return bReturn;
}


//定义解码表

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

};

//解码函数

bool JBase64::Decode(
const BYTE* pByteIn,
int nInLength,
BYTE* pByteOut,
int& nOutLength
)
{
bool bReturn = false;
assert(pByteIn != NULL);
assert(pByteOut != NULL);
assert(nInLength > 0);
assert(nInLength % 4 == 0);
if (pByteIn == NULL || pByteOut == NULL || nInLength % 4 != 0)
{
goto exit0;
}
BYTE tmp[4];
BYTE* pOut = pByteOut;
int nLength = nInLength / 4;
for (int i = 0; i < nLength; i++)
{
tmp[0] = pszDecodeTable[pByteIn[0]];
tmp[1] = pszDecodeTable[pByteIn[1]];
tmp[2] = pszDecodeTable[pByteIn[2]];
tmp[3] = pszDecodeTable[pByteIn[3]];

*pOut++ = ((tmp[0] << 2) | (tmp[1] >> 4));
*pOut++ = ((tmp[1] << 4) | ((tmp[2] == 254 ? 0 : tmp[2] >> 2)));
*pOut++ = (((tmp[2] == 254 ? 0 : tmp[2]<< 6)) | ((tmp[3] == 254 ? 0 : tmp[3])));
pByteIn += 4;
}
*pOut = '/0';
nOutLength = static_cast<int>(pOut - pByteOut);
bReturn = true;
exit0:
return bReturn;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: