编写ATL工程实现ActiveX控件调用cryptoAPI接口(四)------------Base64转码

[cpp]  view plain  copy
 print ?
  1. /* 
  2.  *  
  3.  *  
  4.  * 文件名称:base64.cpp 
  5.  * 摘    要: 
  6.  * 当前版本:1.0 
  7.  * 作    者:周绍禹 
  8.  * 创建日期:2012年3月3日 
  9.  */  
  10.   
  11. #include "stdafx.h"  
  12. #include "base64.h"  
  13. //-----------------------------------------------------------  
  14. // 函数名称:  
  15. //     Base64::base64_encode  
  16. // 参数:  
  17. //    - const BYTE* bytes_to_encode 待编码的字节数组  
  18. //    - int lenth   字节数组大小  
  19. // 返回:  
  20. //     string   base64编码的字符串  
  21. // 说明:  
  22. //     将字节数组以base64编码格式编码字符串  
  23. //-----------------------------------------------------------  
  24. string Base64::base64_encode(const BYTE* bytes_to_encode,int lenth)  
  25. {  
  26.     string ret;    
  27.     int i = 0;    
  28.     int j = 0;    
  29.     unsigned char char_array_3[3];    
  30.     unsigned char char_array_4[4];    
  31.      const string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";    
  32.     while (lenth--) {    
  33.         char_array_3[i++] = *(bytes_to_encode++);    
  34.         if (i == 3) {    
  35.             char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;    
  36.             char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);    
  37.             char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);    
  38.             char_array_4[3] = char_array_3[2] & 0x3f;    
  39.                 
  40.             for(i = 0; (i <4) ; i++)    
  41.                 ret += base64_chars[char_array_4[i]];    
  42.             i = 0;    
  43.         }    
  44.     }    
  45.         
  46.     if (i)    
  47.     {    
  48.         for(j = i; j < 3; j++)    
  49.             char_array_3[j] = '\0';    
  50.             
  51.         char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;    
  52.         char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);    
  53.         char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);    
  54.         char_array_4[3] = char_array_3[2] & 0x3f;    
  55.             
  56.         for (j = 0; (j < i + 1); j++)    
  57.             ret += base64_chars[char_array_4[j]];    
  58.             
  59.         while((i++ < 3))    
  60.             ret += '=';    
  61.             
  62.     }    
  63.         
  64.     return ret;    
  65. }  
  66.   
  67. //-----------------------------------------------------------  
  68. // 函数名称:  
  69. //     Base64::base64_decode  
  70. // 参数:  
  71. //    - const string encoded_string base64编码格式的字符串  
  72. //    - int& length 返回解码后的字节数组大小  
  73. // 返回:  
  74. //     BYTE*    解码后的字节数组  
  75. // 说明:  
  76. //     解码base64编码的字符串  
  77. //-----------------------------------------------------------  
  78. BYTE* Base64::base64_decode(const string encoded_string,int& length)  
  79. {  
  80.     int in_len = encoded_string.size();    
  81.     int i = 0;    
  82.     int j = 0;    
  83.     int in_ = 0;    
  84.     unsigned char char_array_4[4], char_array_3[3];    
  85.     string ret;    
  86.      const string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";    
  87.     while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {    
  88.         char_array_4[i++] = encoded_string[in_]; in_++;    
  89.         if (i ==4) {    
  90.             for (i = 0; i <4; i++)    
  91.                 char_array_4[i] = base64_chars.find(char_array_4[i]);    
  92.                 
  93.             char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);    
  94.             char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);    
  95.             char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];    
  96.                 
  97.             for (i = 0; (i < 3); i++)    
  98.                 ret += char_array_3[i];    
  99.             i = 0;    
  100.         }    
  101.     }    
  102.         
  103.     if (i) {    
  104.         for (j = i; j <4; j++)    
  105.             char_array_4[j] = 0;    
  106.             
  107.         for (j = 0; j <4; j++)    
  108.             char_array_4[j] = base64_chars.find(char_array_4[j]);    
  109.             
  110.         char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);    
  111.         char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);    
  112.         char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];    
  113.             
  114.         for (j = 0; (j < i - 1); j++) ret += char_array_3[j];    
  115.     }    
  116.     length = ret.size();  
  117.     return str2bytes(ret);    
  118. }  
  119.   
  120. //-----------------------------------------------------------  
  121. // 函数名称:  
  122. //     Base64::is_base64  
  123. // 参数:  
  124. //    - unsigned char c 待验证字符  
  125. // 返回:  
  126. //     bool 是否为base64字符  
  127. // 说明:  
  128. //     验证字符是否为base64字符  
  129. //-----------------------------------------------------------  
  130. bool Base64::is_base64(unsigned char c) {  
  131.   return (isalnum(c) || (c == '+') || (c == '/'));  
  132. }  
  133.   
  134. //-----------------------------------------------------------  
  135. // 函数名称:  
  136. //     Base64::str2bytes  
  137. // 参数:  
  138. //    - string target   目标字符串  
  139. // 返回:  
  140. //     BYTE*    字符串的字节数组  
  141. // 说明:  
  142. //     获取字符串的字节数组  
  143. //-----------------------------------------------------------  
  144. BYTE* Base64::str2bytes(string target)  
  145. {  
  146.     int size = target.size();  
  147.     BYTE* u_ch=new BYTE[size + 1];   
  148.     for(int i = 0 ; i < size; i++)  
  149.     {  
  150.         *(u_ch + i) = target[i];  
  151.     }  
  152.     u_ch[size] = '\0';  
  153.     return u_ch;  
  154. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值