Base64加密解密算法的C/C++代码实现

这个Base64是从Live555的代码里面提取出来的,请大家放心使用吧。

 

Base64的加密解密的算法和原理我就不想说了。。。。, 网上有很多介绍原理的文章。

这个算法有啥好处呢?  为什么我们要用它?

 

Base64算法不是为了防止内容被人破解而使用的算法, 因为破解Base64太容易了, 就是简单的字符替换。

 

Base64还有个不好的地方,就是加密后, 长度会变成。

 

那我们为何还要使用Base64 呢 ?  因为Base64可以把乱七八糟的二进制转化为字符串,这个特性在很多地方很有用。

 

 

Base64.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  

 

Base64.cpp 文件

 

 

  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. // implementation  
  20.   
  21. #include "base64.h"  
  22.   
  23. #include <string.h>  
  24.   
  25. static char base64DecodeTable[256];  
  26.   
  27.   
  28. char* strDup(char const* str)   
  29. {  
  30.     if (str == NULL) return NULL;  
  31.     size_t len = strlen(str) + 1;  
  32.     char* copy = new char[len];  
  33.   
  34.     if (copy != NULL)   
  35.     {  
  36.         memcpy(copy, str, len);  
  37.     }  
  38.     return copy;  
  39. }  
  40.   
  41. char* strDupSize(char const* str)   
  42. {  
  43.     if (str == NULL) return NULL;  
  44.     size_t len = strlen(str) + 1;  
  45.     char* copy = new char[len];  
  46.   
  47.     return copy;  
  48. }  
  49.   
  50.   
  51.   
  52. static void initBase64DecodeTable()  
  53. {  
  54.   int i;  
  55.   for (i = 0; i < 256; ++i) base64DecodeTable[i] = (char)0x80;  
  56.       // default value: invalid  
  57.   
  58.   for (i = 'A'; i <= 'Z'; ++i) base64DecodeTable[i] = 0 + (i - 'A');  
  59.   for (i = 'a'; i <= 'z'; ++i) base64DecodeTable[i] = 26 + (i - 'a');  
  60.   for (i = '0'; i <= '9'; ++i) base64DecodeTable[i] = 52 + (i - '0');  
  61.   base64DecodeTable[(unsigned char)'+'] = 62;  
  62.   base64DecodeTable[(unsigned char)'/'] = 63;  
  63.   base64DecodeTable[(unsigned char)'='] = 0;  
  64. }  
  65.   
  66. unsigned char* base64Decode(char* in, unsigned int& resultSize, bool trimTrailingZeros)   
  67. {  
  68.   static bool haveInitedBase64DecodeTable = false;  
  69.   if (!haveInitedBase64DecodeTable)  
  70.   {  
  71.     initBase64DecodeTable();  
  72.     haveInitedBase64DecodeTable = true;  
  73.   }  
  74.   
  75.   unsigned char* out = (unsigned char*)strDupSize(in); // ensures we have enough space  
  76.   int k = 0;  
  77.   int const jMax = strlen(in) - 3;  
  78.      // in case "in" is not a multiple of 4 bytes (although it should be)  
  79.   for (int j = 0; j < jMax; j += 4)   
  80.   {  
  81.     char inTmp[4], outTmp[4];  
  82.     for (int i = 0; i < 4; ++i)   
  83.     {  
  84.       inTmp[i] = in[i+j];  
  85.       outTmp[i] = base64DecodeTable[(unsigned char)inTmp[i]];  
  86.       if ((outTmp[i]&0x80) != 0) outTmp[i] = 0; // pretend the input was 'A'  
  87.     }  
  88.   
  89.     out[k++] = (outTmp[0]<<2) | (outTmp[1]>>4);  
  90.     out[k++] = (outTmp[1]<<4) | (outTmp[2]>>2);  
  91.     out[k++] = (outTmp[2]<<6) | outTmp[3];  
  92.   }  
  93.   
  94.   if (trimTrailingZeros)   
  95.   {  
  96.     while (k > 0 && out[k-1] == '/0') --k;  
  97.   }  
  98.   resultSize = k;  
  99.   unsigned char* result = new unsigned char[resultSize];  
  100.   memmove(result, out, resultSize);  
  101.   delete[] out;  
  102.   
  103.   return result;  
  104. }  
  105.   
  106. static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";  
  107.   
  108. char* base64Encode(char const* origSigned, unsigned origLength)   
  109. {  
  110.   unsigned char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB set  
  111.   if (orig == NULL) return NULL;  
  112.   
  113.   unsigned const numOrig24BitValues = origLength/3;  
  114.   bool havePadding = origLength > numOrig24BitValues*3;  
  115.   bool havePadding2 = origLength == numOrig24BitValues*3 + 2;  
  116.   unsigned const numResultBytes = 4*(numOrig24BitValues + havePadding);  
  117.   char* result = new char[numResultBytes+1]; // allow for trailing '/0'  
  118.   
  119.   // Map each full group of 3 input bytes into 4 output base-64 characters:  
  120.   unsigned i;  
  121.   for (i = 0; i < numOrig24BitValues; ++i)   
  122.   {  
  123.     result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];  
  124.     result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];  
  125.     result[4*i+2] = base64Char[((orig[3*i+1]<<2) | (orig[3*i+2]>>6))&0x3F];  
  126.     result[4*i+3] = base64Char[orig[3*i+2]&0x3F];  
  127.   }  
  128.   
  129.   // Now, take padding into account.  (Note: i == numOrig24BitValues)  
  130.   if (havePadding)   
  131.   {  
  132.     result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];  
  133.     if (havePadding2)  
  134.     {  
  135.       result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];  
  136.       result[4*i+2] = base64Char[(orig[3*i+1]<<2)&0x3F];  
  137.     }   
  138.     else   
  139.     {  
  140.       result[4*i+1] = base64Char[((orig[3*i]&0x3)<<4)&0x3F];  
  141.       result[4*i+2] = '=';  
  142.     }  
  143.     result[4*i+3] = '=';  
  144.   }  
  145.   
  146.   result[numResultBytes] = '/0';  
  147.   return result;  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值