Encryption-基础:base64加解密

环境:vc2003

.h

 1 /**********
 2 This library is free software; you can redistribute it and/or modify it under
 3 the terms of the GNU Lesser General Public License as published by the
 4 Free Software Foundation; either version 2.1 of the License, or (at your
 5 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
 6 
 7 This library is distributed in the hope that it will be useful, but WITHOUT
 8 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 9 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
10 more details.
11 
12 You should have received a copy of the GNU Lesser General Public License
13 along with this library; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
15 **********/
16 // "liveMedia"
17 // Copyright (c) 1996-2010 Live Networks, Inc.  All rights reserved.
18 // Base64 encoding and decoding
19 // C++ header
20 
21 #ifndef _BASE64_HH
22 #define _BASE64_HH
23 
24 //#ifndef _BOOLEAN_HH
25 //#include "Boolean.hh"
26 //#endif
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32     unsigned char* base64Decode(char* in, unsigned int& resultSize, bool trimTrailingZeros = true);
33     // returns a newly allocated array - of size "resultSize" - that
34     // the caller is responsible for delete[]ing.
35 
36     char* base64Encode(char const* orig, unsigned origLength);
37     // returns a 0-terminated string that
38     // the caller is responsible for delete[]ing.
39 
40 
41 #ifdef __cplusplus
42 }
43 #endif
44 
45 #endif

.cpp

  1 #include <string.h>
  2 #include "Base64New.h"
  3 static char base64DecodeTable[256];
  4 
  5 
  6 ////////////////////////////////////////
  7 char* strDup(char const* str) 
  8 {
  9     if (str == NULL) return NULL;
 10     size_t len = strlen(str) + 1;
 11     char* copy = new char[len];
 12 
 13     if (copy != NULL) 
 14     {
 15         memcpy(copy, str, len);
 16     }
 17     return copy;
 18 }
 19 
 20 char* strDupSize(char const* str) 
 21 {
 22     if (str == NULL) return NULL;
 23     size_t len = strlen(str) + 1;
 24     char* copy = new char[len];
 25 
 26     return copy;
 27 }
 28 
 29 
 30 
 31 static void initBase64DecodeTable()
 32 {
 33     int i;
 34     for (i = 0; i < 256; ++i) base64DecodeTable[i] = (char)0x80;
 35     // default value: invalid
 36 
 37     for (i = 'A'; i <= 'Z'; ++i) base64DecodeTable[i] = 0 + (i - 'A');
 38     for (i = 'a'; i <= 'z'; ++i) base64DecodeTable[i] = 26 + (i - 'a');
 39     for (i = '0'; i <= '9'; ++i) base64DecodeTable[i] = 52 + (i - '0');
 40     base64DecodeTable[(unsigned char)'+'] = 62;
 41     base64DecodeTable[(unsigned char)'/'] = 63;
 42     base64DecodeTable[(unsigned char)'='] = 0;
 43 }
 44 
 45 unsigned char* base64Decode(char* in, unsigned int& resultSize, bool trimTrailingZeros) 
 46 {
 47     static bool haveInitedBase64DecodeTable = false;
 48     if (!haveInitedBase64DecodeTable)
 49     {
 50         initBase64DecodeTable();
 51         haveInitedBase64DecodeTable = true;
 52     }
 53 
 54     unsigned char* out = (unsigned char*)strDupSize(in); // ensures we have enough space
 55     int k = 0;
 56     int const jMax = strlen(in) - 3;
 57     // in case "in" is not a multiple of 4 bytes (although it should be)
 58     for (int j = 0; j < jMax; j += 4) 
 59     {
 60         char inTmp[4], outTmp[4];
 61         for (int i = 0; i < 4; ++i) 
 62         {
 63             inTmp[i] = in[i+j];
 64             outTmp[i] = base64DecodeTable[(unsigned char)inTmp[i]];
 65             if ((outTmp[i]&0x80) != 0) outTmp[i] = 0; // pretend the input was 'A'
 66         }
 67 
 68         out[k++] = (outTmp[0]<<2) | (outTmp[1]>>4);
 69         out[k++] = (outTmp[1]<<4) | (outTmp[2]>>2);
 70         out[k++] = (outTmp[2]<<6) | outTmp[3];
 71     }
 72 
 73     if (trimTrailingZeros) 
 74     {
 75         while (k > 0 && out[k-1] == '/0') --k;
 76     }
 77     resultSize = k;
 78     unsigned char* result = new unsigned char[resultSize];
 79     memmove(result, out, resultSize);
 80     delete[] out;
 81 
 82     return result;
 83 }
 84 
 85 static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 86 
 87 char* base64Encode(char  const* origSigned, unsigned origLength) 
 88 {
 89     unsigned char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB set //参数1转换成无符号的
 90     if (orig == NULL) return NULL;
 91 
 92     unsigned const numOrig24BitValues = origLength/3; //numOrig24BitValues保存整除3后的值
 93     bool havePadding = origLength > numOrig24BitValues*3;//判断源字符串长度,大于整除后,再乘3的长度,有大于或等于两种可能。
 94     bool havePadding2 = origLength == numOrig24BitValues*3 + 2;  //整除3后,再乘3,再加2,与源字符串长度比较是否相等。
 95     unsigned const numResultBytes = 4*(numOrig24BitValues + havePadding);  //计算长度,判断整除3后,加0或1,乘4,赋给numResultBytes;
 96     char* result = new char[numResultBytes+1]; // allow for trailing '/0'
 97 
 98     memset(result,0,numResultBytes+1);  //add by wjz20130813
 99 
100     // Map each full group of 3 input bytes into 4 output base-64 characters:
101     unsigned i;
102     for (i = 0; i < numOrig24BitValues; ++i) 
103     {
104         result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];
105         result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];
106         result[4*i+2] = base64Char[((orig[3*i+1]<<2) | (orig[3*i+2]>>6))&0x3F];
107         result[4*i+3] = base64Char[orig[3*i+2]&0x3F];
108     }
109 
110     // Now, take padding into account.  (Note: i == numOrig24BitValues)
111     if (havePadding) 
112     {
113         result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];
114         if (havePadding2)
115         {
116             result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];
117             result[4*i+2] = base64Char[(orig[3*i+1]<<2)&0x3F];
118         } 
119         else 
120         {
121             result[4*i+1] = base64Char[((orig[3*i]&0x3)<<4)&0x3F];
122             result[4*i+2] = '=';
123         }
124         result[4*i+3] = '=';
125     }
126 
127     result[numResultBytes+1] = '/0';  //alter by wjz 20130813
128 
129     return result;
130 }

 

posted @ 2013-08-13 17:32  CPYER  阅读(459)  评论(0编辑  收藏  举报