找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4038|回复: 0

VC下Base64编码及解码程序源代码

[复制链接]
发表于 2012-2-2 19:42:37 | 显示全部楼层 |阅读模式

  1. //
  2. Base64.h: interface for the CBase64
  3. class.
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #if
  7. !defined(AFX_BASE64_H__8D85F486_CD10_4A0A_A689_2299C9DE52CB__INCLUDED_)
  8. #define
  9. AFX_BASE64_H__8D85F486_CD10_4A0A_A689_2299C9DE52CB__INCLUDED_
  10. #if
  11. _MSC_VER > 1000
  12. #pragma once
  13. #endif // _MSC_VER > 1000
  14. #define
  15. DllImport   __declspec( dllimport )
  16. #define DllExport   __declspec( dllexport
  17. )
  18. class
  19. DllExport CBase64  
  20. {
  21. public:
  22. CBase64();
  23. virtual ~CBase64();
  24. public:
  25. int
  26. DecodeBase64(const char* pSrc, unsigned char* pDst, int nSrcLen);
  27. int
  28. EncodeBase64(const unsigned char* pSrc, char* pDst, int nSrcLen, int
  29. nMaxLineLen);
  30. };
  31. #endif
  32. // !defined(AFX_BASE64_H__8D85F486_CD10_4A0A_A689_2299C9DE52CB__INCLUDED_)
  33. //
  34. Base64.cpp: implementation of the CBase64
  35. class.
  36. //
  37. //////////////////////////////////////////////////////////////////////
  38. #include
  39. "stdafx.h"
  40. #include "Base64.h"
  41. #ifdef
  42. _DEBUG
  43. #undef THIS_FILE
  44. static char THIS_FILE[]=__FILE__;
  45. #define new
  46. DEBUG_NEW
  47. #endif
  48. //////////////////////////////////////////////////////////////////////
  49. //
  50. Construction/Destruction
  51. //////////////////////////////////////////////////////////////////////
  52. CBase64::CBase64()
  53. {
  54. }
  55. CBase64::~CBase64()
  56. {
  57. }
  58. /*
  59. Base64是MIME邮件中常用的编码方式之一。它的主要思想是将输入的字符串或数据编码成只含有{'A'-'Z',
  60. 'a'-'z', '0'-'9', '+', '/'}这64个可打印字符的串,故称为“Base64”。
  61. Base64编码的方法是,将输入数据流每次取6
  62. bit,用此6 bit的值(0-63)作为索引去查表,输出相应字符。这样,每3个字节将编码为4个字符(3×8 →
  63. 4×6);不满4个字符的以'='填充。
  64. */
  65. const
  66. char EnBase64Tab[] =
  67. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  68. int
  69. CBase64::EncodeBase64(const unsigned char* pSrc, char* pDst, int nSrcLen, int
  70. nMaxLineLen)
  71. {
  72.     unsigned char c1, c2, c3;    // 输入缓冲区读出3个字节
  73.     int
  74. nDstLen = 0;             // 输出的字符计数
  75.     int nLineLen = 0;            //
  76. 输出的行长度计数
  77.     int nDiv = nSrcLen / 3;      // 输入数据长度除以3得到的倍数
  78.     int nMod =
  79. nSrcLen % 3;      // 输入数据长度除以3得到的余数
  80.     // 每次取3个字节,编码成4个字符
  81.     for
  82. (int i = 0; i < nDiv; i ++)
  83.     {
  84.         // 取3个字节
  85.         c1 =
  86. *pSrc++;
  87.         c2 = *pSrc++;
  88.         c3 = *pSrc++;
  89.         //
  90. 编码成4个字符
  91.         *pDst++ = EnBase64Tab[c1 >> 2];
  92.         *pDst++ =
  93. EnBase64Tab[((c1 << 4) | (c2 >> 4)) & 0x3f];
  94.         *pDst++
  95. = EnBase64Tab[((c2 << 2) | (c3 >> 6)) & 0x3f];
  96.         
  97. *pDst++ = EnBase64Tab[c3 & 0x3f];
  98.         nLineLen += 4;
  99.         
  100. nDstLen += 4;
  101.         // 输出换行?
  102.         if (nLineLen > nMaxLineLen
  103. - 4)
  104.         {
  105.             *pDst++ = '/r';
  106.             *pDst++ =
  107. '/n';
  108.             nLineLen = 0;
  109.             nDstLen += 2;
  110.         
  111. }
  112.     }
  113.     // 编码余下的字节
  114.     if (nMod == 1)
  115.     {
  116.         c1 =
  117. *pSrc++;
  118.         *pDst++ = EnBase64Tab[(c1 & 0xfc) >>
  119. 2];
  120.         *pDst++ = EnBase64Tab[((c1 & 0x03) << 4)];
  121.         
  122. *pDst++ = '=';
  123.         *pDst++ = '=';
  124.         nLineLen += 4;
  125.         
  126. nDstLen += 4;
  127.     }
  128.     else if (nMod == 2)
  129.     {
  130.         c1 =
  131. *pSrc++;
  132.         c2 = *pSrc++;
  133.         *pDst++ = EnBase64Tab[(c1 &
  134. 0xfc) >> 2];
  135.         *pDst++ = EnBase64Tab[((c1 & 0x03) << 4)
  136. | ((c2 & 0xf0) >> 4)];
  137.         *pDst++ = EnBase64Tab[((c2 &
  138. 0x0f) << 2)];
  139.         *pDst++ = '=';
  140.         nDstLen += 4;
  141.    
  142. }
  143.     // 输出加个结束符
  144.     *pDst = '/0';
  145.     return
  146. nDstLen;
  147. }
  148. /*
  149. Base64解码方法中,最简单的也是查表法:将64个可打印字符的值作为索引,查表得到的值(范围为0-63)依次连起来,拼凑成字节形式输出,就得到解码结果。
  150. */
  151. const
  152. char DeBase64Tab[] =
  153. {
  154.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  155. 0, 0, 0, 0, 0, 0, 0, 0,
  156.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  157. 0, 0, 0,
  158.     62,        // '+'
  159.     0, 0, 0,
  160.     63,        //
  161. '/'
  162.     52, 53, 54, 55, 56, 57, 58, 59, 60, 61,        // '0'-'9'
  163.     0,
  164. 0, 0, 0, 0, 0, 0,
  165.     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  166.     13,
  167. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,        // 'A'-'Z'
  168.     0, 0,
  169. 0, 0, 0, 0,
  170.     26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  171.    
  172. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,        //
  173. 'a'-'z'
  174. };
  175. int CBase64::DecodeBase64(const char* pSrc, unsigned char*
  176. pDst, int nSrcLen)
  177. {
  178.     int nDstLen;            // 输出的字符计数
  179.     int
  180. nValue;             // 解码用到的长整数
  181.     int i;
  182.     i = 0;
  183.     nDstLen
  184. = 0;
  185.     // 取4个字符,解码到一个长整数,再经过移位得到3个字节
  186.     while (i <
  187. nSrcLen)
  188.     {
  189.         if (*pSrc != '/r' &&
  190. *pSrc!='/n')
  191.         {
  192.             nValue = DeBase64Tab[*pSrc++] <<
  193. 18;
  194.             nValue += DeBase64Tab[*pSrc++] << 12;
  195.             
  196. *pDst++ = (nValue & 0x00ff0000) >> 16;
  197.             
  198. nDstLen++;
  199.             if (*pSrc != '=')
  200.             
  201. {
  202.                 nValue += DeBase64Tab[*pSrc++] <<
  203. 6;
  204.                 *pDst++ = (nValue & 0x0000ff00) >>
  205. 8;
  206.                 nDstLen++;
  207.                 if (*pSrc !=
  208. '=')
  209.                 {
  210.                     nValue +=
  211. DeBase64Tab[*pSrc++];
  212.                     *pDst++ =nValue &
  213. 0x000000ff;
  214.                     nDstLen++;
  215.                
  216. }
  217.             }
  218.             i += 4;
  219.         }
  220.         
  221. else        // 回车换行,跳过
  222.         {
  223.             pSrc++;
  224.             
  225. i++;
  226.         }
  227.      }
  228.     // 输出加个结束符
  229.     *pDst =
  230. '/0';
  231.     return nDstLen;
  232. }
复制代码

转自:http://blog.csdn.net/zhaoyawei/article/details/670299


您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-2 12:58 , Processed in 0.023179 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表