Rover12421's Blog

The End.

VS2010下Unicode的Base64加密解密程序

很早就弄好的一份代码,发上来之前,又修改了几处(Analysis 的警告)。实际上也就是字符转换,然后使用了别人原有的Base64加解密程序。界面如下:

image

加解密代码放在code.cpp和code.h中,如果想用在其他地方,只要包含这个两个文件就行了。

Code.cpp
  1. #include "stdafx.h"
  2. #include "Code.h"
  3.  
  4. int CStringEncodeBase64(const CString &szInput, CString &szOutput)
  5. {
  6.     int nRet = 0;
  7.     int nLeng = 0;
  8.     nLeng    = (szInput.GetLength() + 1)*sizeof(TCHAR);
  9.     char *pInput = new char[nLeng];
  10.     ZeroMemory(pInput, nLeng);
  11.     char *pOutput = new char[nLeng];    
  12.     ZeroMemory(pOutput, nLeng);
  13.     //USES_CONVERSION;
  14.     strcpy_s(pInput, nLeng, CT2A(szInput));
  15.     nRet = base64_encode((void *)pInput, strlen(pInput), &pOutput);
  16.     szOutput = CA2T(pOutput);
  17.     delete []pInput;
  18.     delete []pOutput;
  19.     return nRet;
  20. }
  21.  
  22. int CStringDecodeBase64(const CString &szInput, CString &szOutput)
  23. {
  24.     int nRet = 0;
  25.     int nLeng = 0;
  26.     nLeng    = (szInput.GetLength() + 1)*sizeof(TCHAR);
  27.     char *pInput = new char[nLeng];
  28.     ZeroMemory(pInput, nLeng);
  29.     char *pOutput = new char[nLeng];    
  30.     ZeroMemory(pOutput, nLeng);
  31.     //USES_CONVERSION;
  32.     strcpy_s(pInput, nLeng, CT2A(szInput));
  33.     nRet = base64_decode(pInput, pOutput);
  34.     szOutput = CA2T(pOutput);
  35.     delete [] pInput;
  36.     delete [] pOutput;
  37.     return nRet;
  38. }
  39.  
  40. int base64_encode(const void *data, const int size, char **str)
  41. {
  42.     char *s, *p;
  43.     int i;
  44.     int c;
  45.     const unsigned char *q;
  46.  
  47.     p = s = (char *) malloc(size * 4 / 3 + 4);
  48.     if (p == NULL)
  49.     {
  50.         return -1;
  51.     }
  52.     q = (const unsigned char *) data;
  53.     i = 0;
  54.     for (i = 0; i < size;)
  55.     {
  56.         c = q[i++];
  57.         c *= 256;
  58.         if (i < size)
  59.         {
  60.             c += q[i];
  61.         }
  62.         i++;
  63.         c *= 256;
  64.         if (i < size)
  65.         {
  66.             c += q[i];
  67.         }
  68.         i++;
  69.         p[0] = base64_chars[(c & 0x00fc0000) >> 18];
  70.         p[1] = base64_chars[(c & 0x0003f000) >> 12];
  71.         p[2] = base64_chars[(c & 0x00000fc0) >> 6];
  72.         p[3] = base64_chars[(c & 0x0000003f) >> 0];
  73.         if (i > size)
  74.         {
  75.             p[3] = '=';
  76.         }
  77.         if (i > size + 1)
  78.         {
  79.             p[2] = '=';
  80.         }
  81.         p += 4;
  82.     }
  83.  
  84.     *p = 0;
  85.     *str = s;
  86.     return strlen(s);
  87. }
  88.  
  89.  
  90. static int pos(const char c)
  91. {
  92.     char *p;
  93.     for (p = base64_chars; *p; p++)
  94.     {
  95.         if (*p == c)
  96.         {
  97.             return p - base64_chars;
  98.         }
  99.     }
  100.     return -1;
  101. }
  102.  
  103. static unsigned int token_decode(const char *token)
  104. {
  105.     int i;
  106.     unsigned int val = 0;
  107.     int marker = 0;
  108.     if (strlen(token) < 4)
  109.     {
  110.         return DECODE_ERROR;
  111.     }
  112.     for (i = 0; i < 4; i++)
  113.     {
  114.         val *= 64;
  115.         if (token[i] == '=')
  116.         {
  117.             marker++;
  118.         }
  119.         else
  120.         {
  121.             if (marker > 0)
  122.             {
  123.                 return DECODE_ERROR;
  124.             }
  125.             else
  126.             {
  127.                 val += pos(token[i]);
  128.             }
  129.         }            
  130.     }
  131.     if (marker > 2)
  132.     {
  133.         return DECODE_ERROR;
  134.     }
  135.     return (marker << 24) | val;
  136. }
  137.  
  138. int base64_decode(const char *str, char *data)
  139. {
  140.     const char *p;
  141.     unsigned char *q;
  142.  
  143.     q = (unsigned char *)data;
  144.     for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4)
  145.     {
  146.         unsigned int val = token_decode(p);
  147.         unsigned int marker = (val >> 24) & 0xff;
  148.         if (val == DECODE_ERROR)
  149.         {
  150.             return -1;
  151.         }
  152.         *q++ = (val >> 16) & 0xff;
  153.         if (marker < 2)
  154.         {
  155.             *q++ = (val >> 8) & 0xff;
  156.         }
  157.         if (marker < 1)
  158.         {
  159.             *q++ = val & 0xff;
  160.         }
  161.     }
  162.     return q - (unsigned char *) data;
  163. }

 

Code.h
  1. #pragma once
  2. #define DECODE_ERROR 0xffffffff
  3. static char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  4.  
  5. int CStringEncodeBase64(const CString &szInput, CString &szOutput);
  6. int CStringDecodeBase64(const CString &szInput, CString &szOutput);
  7.  
  8. int base64_encode(const void *data, const int size, char **str);
  9. static int pos(const char c);
  10. static unsigned int token_decode(const char *token);
  11. int base64_decode(const char *str, char *data);

代码是在VS2010中开启 Code Analysis 下无警告编译通过的。

Comments