C++ Base64算法

 1 #ifndef BASE_DATA
 2 #include <string>
 3 
 4 class BaseData
 5 {
 6 public:
 7     BaseData(void);
 8     ~BaseData(void);
 9 
10     std::string saveData(char const* bytes_to_encode,unsigned int in_len);
11     std::string parseData(std::string const& encoded_string);
12 
13 };
14 
15 #endif 
  1 #include "BaseData.h"
  2 
  3 static const std::string dataChars = 
  4 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  5 
  6 
  7 BaseData::BaseData(void)
  8 {
  9 }
 10 
 11 BaseData::~BaseData(void)
 12 {
 13 }
 14 static inline bool isData(unsigned char c)
 15 {
 16     return (isalnum(c) || (c == '+') || (c == '/'));
 17 }
 18 /*======================================
 19 函数名:saveData
 20 参数:  char const* bytes_to_encode--需要编码的目标字符串
 21         unsigned int in_len --- 目标字符串长度
 22 功能:对字符串数据进行编码
 23 返回值:编码后的数据
 24 ======================================*/
 25 std::string BaseData::saveData(char const* bytes_to_encode,unsigned int in_len)
 26 {
 27     std::string ret;
 28     int i = 0;
 29     int j = 0;
 30     unsigned char char_array_3[3];
 31     unsigned char char_array_4[4];
 32 
 33     while(in_len--)
 34     {
 35         char_array_3[i++] = *(bytes_to_encode++);
 36         if (i == 3)
 37         {
 38             char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
 39             char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
 40             char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
 41             char_array_4[3] = char_array_3[2] & 0x3f;
 42 
 43             for(int i = 0; (i < 4); i++)
 44                 ret += dataChars[char_array_4[i]];
 45             i = 0;
 46         }
 47     }
 48     if (i)
 49     {
 50         for (j = i; j < 3; j++)
 51         {
 52             char_array_3[j] = '\0';
 53         }
 54         char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
 55         char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
 56         char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
 57         char_array_4[3] = char_array_3[2] & 0x3f;
 58         for (j = 0; j < i + 1; j++)
 59         {
 60             ret += dataChars[char_array_4[j]];
 61         }
 62         while ((i++ < 3))
 63         {
 64             ret += "=";
 65         }
 66     }
 67     return ret;
 68 }
 69 
 70 /*======================================
 71 函数名:parseData
 72 参数:std::string const& encoded_string
 73             需要编码的目标字符串
 74 功能:对字符串数据进行解码
 75 返回值:解码后的数据
 76 ======================================*/
 77 std::string BaseData::parseData(std::string const& encoded_string)
 78 {
 79     int in_len = encoded_string.size();
 80     int i = 0;
 81     int j = 0;
 82     int in_ = 0;
 83     unsigned char char_array_4[4],char_array_3[3];
 84     std::string ret;
 85     while (in_len-- && (encoded_string[in_] != '=') && isData(encoded_string[in_]))
 86     {
 87         char_array_4[i++] = encoded_string[in_];
 88         in_ ++;
 89         if (i == 4)
 90         {
 91             for (i = 0; i < 4; i++)
 92             {
 93                 char_array_4[i] = dataChars.find(char_array_4[i]);
 94             }
 95             char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
 96             char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
 97             char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
 98             for (i = 0; i < 3; i++)
 99                 ret += char_array_3[i];
100             i = 0;
101         }
102     }
103     if (i)
104     {
105         for (j = i;j < 4; j++)
106             char_array_4[j] = 0;
107         for (j = 0; j < 4; j++)
108             char_array_4[j] = dataChars.find(char_array_4[j]);
109         char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
110         char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
111         char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
112 
113         for (j = 0; (j < i - 1); j++)
114         {
115             ret += char_array_3[j];
116         }
117     }
118     return ret;
119 }
 1 //////////////////////////////////////////////////////////////////////////
 2         //对base64编码的使用
 3         BaseData bd;
 4         std::string sValue = "newlist";
 5         //对存储的数据进行编码
 6         std::string saveBaseString = bd.saveData(sValue.c_str(),sValue.length());
 7         //将编码后的数据进行存储
 8         CCUserDefault::sharedUserDefault()->setStringForKey("Base",saveBaseString);
 9         CCUserDefault::sharedUserDefault()->flush();
10 
11         //从存储中获取编码的数据
12         std::string loadBaseString = CCUserDefault::sharedUserDefault()->getStringForKey("Base");
13         //对编码数据进行解码得到真是数据
14         std::string trueValue = bd.parseData(loadBaseString);
15 
16         //用以观察数据
17         CCLabelTTF* label = CCLabelTTF::create("","Helvetica",20);
18         label->setPosition(ccp(240,185));
19         addChild(label);
20         std::string logStr = "编码前的数据:";
21         logStr += sValue;
22         logStr += "编码后的数据:";
23         logStr += saveBaseString;
24         GBKToUTF(logStr);
25         label->setString(logStr.c_str());
26 
27         CCLabelTTF* label2 = CCLabelTTF::create("","Helvetica",20);
28         label2->setPosition(ccp(240,140));
29         addChild(label2);
30         logStr = "解码前的数据:";
31         logStr += loadBaseString;
32         logStr += "解码后的数据:";
33         logStr += trueValue;
34         GBKToUTF(logStr);
35         label2->setString(logStr.c_str());

 

你可能感兴趣的:(base64)