const char DeBase64Tab[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62, // '+'
0, 0, 0,
63, // '/'
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
0, 0, 0, 0, 0, 0, 0,
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, // 'A'-'Z'
0, 0, 0, 0, 0, 0,
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, // 'a'-'z'
};
int DecodeBase64(const char* pSrc, unsigned char* pDst, int nSrcLen)
{
int nDstLen; // 输出的字符计数
int nValue; // 解码用到的长整数
int i;
i = 0;
nDstLen = 0;
// 取4个字符,解码到一个长整数,再经过移位得到3个字节
while (i < nSrcLen)
{
if (*pSrc != '\r' && *pSrc!='\n')
{
nValue = DeBase64Tab[*pSrc++] << 18;
nValue += DeBase64Tab[*pSrc++] << 12;
*pDst++ = (nValue & 0x00ff0000) >> 16;
nDstLen++;
if (*pSrc != '=')
{
nValue += DeBase64Tab[*pSrc++] << 6;
*pDst++ = (nValue & 0x0000ff00) >> 8;
nDstLen++;
if (*pSrc != '=')
{
nValue += DeBase64Tab[*pSrc++];
*pDst++ =nValue & 0x000000ff;
nDstLen++;
}
}
i += 4;
}
else // 回车换行,跳过
{
pSrc++;
i++;
}
}
// 输出加个结束符
*pDst = '\0';
return nDstLen;
}
加密后编码 Q U J D
二进制 81 85 74 68
查表2 16 20 9 3
二进制 010000 010100 001001 000011
合并移位 01000001 01000010 01000011
还原码 A B C