当前位置:   article > 正文

使用C++对数据进行Base64加密解密_c++ base64加密

c++ base64加密

定义加密选项

  1. enum Base64Option {
  2. Base64Encoding = 0,
  3. Base64UrlEncoding = 1,
  4. KeepTrailingEquals = 0,
  5. OmitTrailingEquals = 2
  6. };

对数据的base64加密

  1. std::string encode_base64(const std::string sourceData, int options = Base64Encoding)
  2. {
  3. const char alphabet_base64[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
  4. "ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
  5. const char alphabet_base64url[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
  6. "ghijklmn" "opqrstuv" "wxyz0123" "456789-_";
  7. const char *const alphabet = options & Base64UrlEncoding ? alphabet_base64url : alphabet_base64;
  8. const char padchar = '=';
  9. int padlen = 0;
  10. std::string tmp;
  11. tmp.resize((sourceData.size() + 2) / 3 * 4);
  12. int i = 0;
  13. char *out = &tmp[0];
  14. while (i < sourceData.size()) {
  15. // encode 3 bytes at a time
  16. int chunk = 0;
  17. chunk |= int(uchar(sourceData.data()[i++])) << 16;
  18. if (i == sourceData.size()) {
  19. padlen = 2;
  20. } else {
  21. chunk |= int(uchar(sourceData.data()[i++])) << 8;
  22. if (i == sourceData.size())
  23. padlen = 1;
  24. else
  25. chunk |= int(uchar(sourceData.data()[i++]));
  26. }
  27. int j = (chunk & 0x00fc0000) >> 18;
  28. int k = (chunk & 0x0003f000) >> 12;
  29. int l = (chunk & 0x00000fc0) >> 6;
  30. int m = (chunk & 0x0000003f);
  31. *out++ = alphabet[j];
  32. *out++ = alphabet[k];
  33. if (padlen > 1) {
  34. if ((options & OmitTrailingEquals) == 0)
  35. *out++ = padchar;
  36. } else {
  37. *out++ = alphabet[l];
  38. }
  39. if (padlen > 0) {
  40. if ((options & OmitTrailingEquals) == 0)
  41. *out++ = padchar;
  42. } else {
  43. *out++ = alphabet[m];
  44. }
  45. }
  46. assert((options & OmitTrailingEquals) || (out == tmp.size() + tmp.data()));
  47. if (options & OmitTrailingEquals)
  48. tmp.resize(out - tmp.data());
  49. return tmp;
  50. }

参数sourceData代表要被加密的数据, options表示加密选项flags 默认0

然后是对数据的base64解密

  1. std::string decode_base64(const std::string sourceData, int options = Base64Encoding)
  2. {
  3. unsigned int buf = 0;
  4. int nbits = 0;
  5. std::string tmp;
  6. tmp.resize((sourceData.size() * 3) / 4);
  7. int offset = 0;
  8. for (int i = 0; i < sourceData.size(); ++i) {
  9. int ch = sourceData.at(i);
  10. int d;
  11. if (ch >= 'A' && ch <= 'Z')
  12. d = ch - 'A';
  13. else if (ch >= 'a' && ch <= 'z')
  14. d = ch - 'a' + 26;
  15. else if (ch >= '0' && ch <= '9')
  16. d = ch - '0' + 52;
  17. else if (ch == '+' && (options & Base64UrlEncoding) == 0)
  18. d = 62;
  19. else if (ch == '-' && (options & Base64UrlEncoding) != 0)
  20. d = 62;
  21. else if (ch == '/' && (options & Base64UrlEncoding) == 0)
  22. d = 63;
  23. else if (ch == '_' && (options & Base64UrlEncoding) != 0)
  24. d = 63;
  25. else
  26. d = -1;
  27. if (d != -1) {
  28. buf = (buf << 6) | d;
  29. nbits += 6;
  30. if (nbits >= 8) {
  31. nbits -= 8;
  32. tmp[offset++] = buf >> nbits;
  33. buf &= (1 << nbits) - 1;
  34. }
  35. }
  36. }
  37. tmp.resize(offset);
  38. return tmp;
  39. }

参数sourceData代表要被解密的数据, options表示加密选项flags,默认0

让我们来看看运行结果:

  1. std::string srcData = "ABCDEFGHIGKLMN...+++---000123456789~!@#$%^&*()_+";
  2. std::string srcData_base64 = encode_base64(srcData);
  3. std::string dstData_base64 = decode_base64(srcData_base64);
  4. std::cout<< "src data:" << srcData << std::endl;
  5. std::cout<< "to base64:" << srcData_base64 << std::endl;
  6. std::cout <<"from base64:" << dstData_base64 << std::endl;
  7. //url
  8. srcData = "https://mp.csdn.net/postedit/83345819";
  9. srcData_base64 = encode_base64(srcData, Base64UrlEncoding & OmitTrailingEquals);
  10. dstData_base64 = decode_base64(srcData_base64, Base64UrlEncoding & OmitTrailingEquals);
  11. std::cout<< "src data:" << srcData << std::endl;
  12. std::cout<< "to base64:" << srcData_base64 << std::endl;
  13. std::cout <<"from base64:" << dstData_base64 << std::endl;

得到结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/523020
推荐阅读
相关标签
  

闽ICP备14008679号