BASE64编码和解码VC代码函数
发布时间:2017年9月15日 作者:未知 查看次数:989
自:http://www.jb51.net/article/74722.htm BASE64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本。完整的BASE64定义可见 RFC1421和 RFC2045。编码后的数据比原始数据略长,为原来的4/3。在电子邮件中,根据RFC822规定,每76个字符,还需要加上一个回车换行。 转换的时候,将三个byte的数据,先后放入一个24bit的缓冲区中,先来的byte占高位。数据不足3byte的话,于缓冲区中剩下的Bit用0补足。然后,每次取出6个bit,按照其值选择ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作为编码后的输出。不断进行,直到全部输入数据转换完成。如果最后剩下两个输入数据,在编码结果后加1个“=”;如果最后剩下一个输入数据,编码结果后加2个“=”;如果没有剩下任何数据,就什么都不要加,这样才可以保证资料还原的正确性。
开发语言:C/C++ 实现功能:实现BASE64编码和解码,提供如下接口函数:
====================== BASE64_API.h 文件内容
==============
================================= 采用以上方法就可以将二进制数据转换成可见字符进行传递就可以了. 那么如何使用呢?举以下两个例子 第一个:将一个图片转换成 txt 文本 并保存起来 //选择一个图像文件,将它转为 文本保存至 _T("D:\\2.txt" void CTextPicDlg::OnBnClickedButton2() { // TODO: 在此添加控件通知处理程序代码 CFileDialog file(TRUE,".jpg",""); if (file.DoModal() == IDOK) { CFile data(file.GetPathName(), CFile::modeReadWrite); int len = data.GetLength(); BYTE *dv; dv = (BYTE *)malloc(len*sizeof(BYTE)); data.Read(dv, len); data.Close(); int slen = (len / 3) * 4; slen += 10; TCHAR * tc; tc = (TCHAR *)malloc(slen); slen = BASE64_Encode(dv, len, tc); CFile save(_T("D:\\2.txt"), CFile::modeCreate | CFile::modeWrite); save.Write(tc, slen); save.Close(); free(tc); free(dv); } } 第二个例子,将一个文本文件还原为一个图像 void CTextPicDlg::OnBnClickedButton3() { // TODO: 在此添加控件通知处理程序代码 CFileDialog file(TRUE, ".txt", ""); if (file.DoModal() == IDOK) { CFile data(file.GetPathName(), CFile::modeReadWrite); int len = data.GetLength(); TCHAR *dv; dv = (TCHAR *)malloc(len*sizeof(TCHAR)); data.Read(dv, len); data.Close(); int slen = (len / 4) * 3; slen += 10; BYTE * tc; tc = (BYTE *)malloc(slen); BASE64_Decode(dv, len, tc); //直接在内存里面构建CIMAGE,需要使用IStream接口,如何使用 //构建内存环境 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, slen); void * pData = GlobalLock(hGlobal); memcpy(pData, tc, slen); // 拷贝位图数据进去 GlobalUnlock(hGlobal); // 创建IStream IStream * pStream = NULL; if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) != S_OK) return ; // 使用CImage加载位图内存 CImage img; if (SUCCEEDED(img.Load(pStream)) ) { CClientDC dc(this); //使用内在中构造的图像 直接在对话框上绘图 img.Draw(dc.m_hDC, 0, 0, 500, 300); } //释放内存 pStream->Release(); GlobalFree(hGlobal); //如果要保存图像文件的话,那就使用下面的代码 //CFileDialog savefile(FALSE, ".jpg", ""); //if (savefile.DoModal()==IDOK) //{ // CFile save(savefile.GetPathName(), CFile::modeCreate | CFile::modeWrite); // save.Write(tc, slen); // save.Close(); //} free(tc); free(dv); } } 至此,利用Base64转码的方式,来显示保存显示图片的方法,就算是成功了! 我们再来看一个base64编码解码的例子 首先是编码 const BYTE Base64ValTab[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define AVal(x) Base64ValTab[x] int CSeeBase64Dlg::EncodeBase64(char * pInput, char * pOutput) { int i = 0; int loop = 0; int remain = 0; int iDstLen = 0; int iSrcLen = (int)strlen(pInput);
loop = iSrcLen/3; remain = iSrcLen%3;
// also can encode native char one by one as decode method // but because all of char in native string is to be encoded so encode 3-chars one time is easier.
for (i=0; i < loop; i++) { BYTE a1 = (pInput[i*3] >> 2); BYTE a2 = ( ((pInput[i*3] & 0x03) << 4) | (pInput[i*3+1] >> 4) ); BYTE a3 = ( ((pInput[i*3+1] & 0x0F) << 2) | ((pInput[i*3+2] & 0xC0) >> 6) ); BYTE a4 = (pInput[i*3+2] & 0x3F);
pOutput[i*4] = AVal(a1); pOutput[i*4+1] = AVal(a2); pOutput[i*4+2] = AVal(a3); pOutput[i*4+3] = AVal(a4); }
iDstLen = i*4;
if (remain == 1) { // should pad two equal sign i = iSrcLen-1; BYTE a1 = (pInput[i] >> 2); BYTE a2 = ((pInput[i] & 0x03) << 4);
pOutput[iDstLen++] = AVal(a1); pOutput[iDstLen++] = AVal(a2); pOutput[iDstLen++] = '='; pOutput[iDstLen++] = '='; pOutput[iDstLen] = 0x00; } else if (remain == 2) { // should pad one equal sign i = iSrcLen-2; BYTE a1 = (pInput[i] >> 2); BYTE a2 = ( ((pInput[i] & 0x03) << 4) | (pInput[i+1] >> 4)); BYTE a3 = ( (pInput[i+1] & 0x0F) << 2);
pOutput[iDstLen++] = AVal(a1); pOutput[iDstLen++] = AVal(a2); pOutput[iDstLen++] = AVal(a3); pOutput[iDstLen++] = '='; pOutput[iDstLen] = 0x00; } else { // just division by 3 pOutput[iDstLen] = 0x00; }
return iDstLen; } 下面是解码 const BYTE Base64IdxTab[128] = { 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,255,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 };
#define BVal(x) Base64IdxTab[x]
int CSeeBase64Dlg::DecodeBase64(char * pInput, char * pOutput) { int i = 0; int iCnt = 0; int iSrcLen = (int)strlen(pInput);
char * p = pOutput;
for (i=0; i < iSrcLen; i++) { if (pInput[i] > 127) continue; if (pInput[i] == '=') return p-pOutput+1;
BYTE a = BVal(pInput[i]); if (a == 255) continue;
switch (iCnt) { case 0: { *p = a << 2; iCnt++; } break;
case 1: { *p++ |= a >> 4; *p = a << 4; iCnt++; } break;
case 2: { *p++ |= a >> 2; *p = a << 6; iCnt++; } break; case 3: { *p++ |= a; iCnt = 0; } break; } }
*p = 0x00; return p-pOutput; } =================================== 函数已测试 Base64_Encode Base64_Decode 例子供参考
|
|
|