• C++ 使用base64进行编码和解码


    1、base64编码的作用:

            base64编码的东西,任何人都可以解码,所以称之为加密和解密是不合适的,其作用是:便于数据再网络上的传输。举个简单的例子,你使用SMTP协议 (Simple Mail Transfer Protocol 简单邮件传输协议)来发送邮件,因为这个协议是基于文本的协议,所以,如果邮件中包含一幅图片,我们知道图片的存储格式是二进制数据(binary data)而非文本格式,我们必须将二进制的数据编码成文本格式,这时候Base 64 Encoding就派上用场了。

    2、C++实现的base64编码类及其使用方法:

    1)、类:

    1. #include "stdafx.h"
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. using namespace std;
    8. namespace {
    9. static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    10. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    11. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    12. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    13. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    14. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    15. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
    16. '4', '5', '6', '7', '8', '9', '+', '/' };
    17. static char *decoding_table = NULL;
    18. static int mod_table[] = { 0, 2, 1 };
    19. void build_decoding_table()
    20. {
    21. decoding_table = (char*)malloc(256);
    22. for (int i = 0; i < 64; i++)
    23. decoding_table[(unsigned char)encoding_table[i]] = i;
    24. }
    25. char* base64_encode(const unsigned char *data, size_t input_length, size_t *output_length)
    26. {
    27. *output_length = 4 * ((input_length + 2) / 3);
    28. char *encoded_data = (char*)malloc(*output_length + 1);
    29. if (encoded_data == NULL) return NULL;
    30. memset(encoded_data, 0, *output_length + 1);
    31. for (int i = 0, j = 0; i < input_length;) {
    32. uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
    33. uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
    34. uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
    35. uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
    36. encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
    37. encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
    38. encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
    39. encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
    40. }
    41. for (int i = 0; i < mod_table[input_length % 3]; i++)
    42. encoded_data[*output_length - 1 - i] = '=';
    43. return encoded_data;
    44. }
    45. unsigned char* base64_decode(const char *data, size_t input_length, size_t *output_length)
    46. {
    47. if (decoding_table == NULL) build_decoding_table();
    48. if (input_length % 4 != 0) return NULL;
    49. *output_length = input_length / 4 * 3;
    50. if (data[input_length - 1] == '=') (*output_length)--;
    51. if (data[input_length - 2] == '=') (*output_length)--;
    52. unsigned char *decoded_data = (unsigned char*)malloc(*output_length);
    53. if (decoded_data == NULL) return NULL;
    54. memset(decoded_data, 0, *output_length);
    55. for (int i = 0, j = 0; i < input_length;) {
    56. uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
    57. uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
    58. uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
    59. uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
    60. uint32_t triple = (sextet_a << 3 * 6)
    61. + (sextet_b << 2 * 6)
    62. + (sextet_c << 1 * 6)
    63. + (sextet_d << 0 * 6);
    64. if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
    65. if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
    66. if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
    67. }
    68. return decoded_data;
    69. }
    70. void base64_cleanup()
    71. {
    72. free(decoding_table);
    73. }
    74. } // namespace

    2)、使用注意:

    特别要注意的是:在编码前,我们应该将编码内容转换成通用的UTF8格式,解码出来的内容也要转换成自己的编码格式来显示,比如:unicode/ansi等格式。否则的话,对于中文内容,如果和其他语言进行中文内容的传输操作,比如java,它解码出来的内容会是乱码。

    3)、编码转换函数,由于我本地是ANSI编码格式,所以下面给出ANSI与UTF8编码格式的相互转换函数,如果你本地使用的是UNICODE编码格式,就要用到UNICODE与UTF8之间的相互转换:

    1. std::string ANSItoUTF8(const char * ansi)
    2. {
    3. int len = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
    4. wchar_t* wstr = new wchar_t[len+1];
    5. memset(wstr, 0, len+1);
    6. MultiByteToWideChar(CP_ACP, 0, ansi, -1, wstr, len);
    7. len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
    8. char* str = new char[len+1];
    9. memset(str, 0, len+1);
    10. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
    11. if(wstr) delete[] wstr;
    12. std::string ret = str;
    13. if(str) delete[] str;
    14. return ret;
    15. }
    16. std::string UTF8toANSI(const char* utf8)
    17. {
    18. int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    19. wchar_t* wstr = new wchar_t[len+1];
    20. memset(wstr, 0, len+1);
    21. MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    22. len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    23. char* str = new char[len+1];
    24. memset(str, 0, len+1);
    25. WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    26. if(wstr) delete[] wstr;
    27. std::string ret = str;
    28. if(str) delete[] str;
    29. return ret;
    30. }

    4)、实例:

    1. // base64Test.cpp : 定义控制台应用程序的入口点?
    2. //
    3. #include "stdafx.h"
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. using namespace std;
    10. namespace {
    11. static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    12. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    13. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    14. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    15. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    16. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    17. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
    18. '4', '5', '6', '7', '8', '9', '+', '/' };
    19. static char *decoding_table = NULL;
    20. static int mod_table[] = { 0, 2, 1 };
    21. void build_decoding_table()
    22. {
    23. decoding_table = (char*)malloc(256);
    24. for (int i = 0; i < 64; i++)
    25. decoding_table[(unsigned char)encoding_table[i]] = i;
    26. }
    27. char* base64_encode(const unsigned char *data, size_t input_length, size_t *output_length)
    28. {
    29. *output_length = 4 * ((input_length + 2) / 3);
    30. char *encoded_data = (char*)malloc(*output_length);
    31. if (encoded_data == NULL) return NULL;
    32. for (int i = 0, j = 0; i < input_length;) {
    33. uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
    34. uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
    35. uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
    36. uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
    37. encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
    38. encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
    39. encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
    40. encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
    41. }
    42. for (int i = 0; i < mod_table[input_length % 3]; i++)
    43. encoded_data[*output_length - 1 - i] = '=';
    44. return encoded_data;
    45. }
    46. unsigned char* base64_decode(const char *data, size_t input_length, size_t *output_length)
    47. {
    48. if (decoding_table == NULL) build_decoding_table();
    49. if (input_length % 4 != 0) return NULL;
    50. *output_length = input_length / 4 * 3;
    51. if (data[input_length - 1] == '=') (*output_length)--;
    52. if (data[input_length - 2] == '=') (*output_length)--;
    53. unsigned char *decoded_data = (unsigned char*)malloc(*output_length);
    54. if (decoded_data == NULL) return NULL;
    55. for (int i = 0, j = 0; i < input_length;) {
    56. uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
    57. uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
    58. uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
    59. uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
    60. uint32_t triple = (sextet_a << 3 * 6)
    61. + (sextet_b << 2 * 6)
    62. + (sextet_c << 1 * 6)
    63. + (sextet_d << 0 * 6);
    64. if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
    65. if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
    66. if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
    67. }
    68. return decoded_data;
    69. }
    70. void base64_cleanup()
    71. {
    72. free(decoding_table);
    73. }
    74. } // namespace
    75. std::string ANSItoUTF8(const char * ansi)
    76. {
    77. int len = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
    78. wchar_t* wstr = new wchar_t[len+1];
    79. memset(wstr, 0, len+1);
    80. MultiByteToWideChar(CP_ACP, 0, ansi, -1, wstr, len);
    81. len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
    82. char* str = new char[len+1];
    83. memset(str, 0, len+1);
    84. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
    85. if(wstr) delete[] wstr;
    86. std::string ret = str;
    87. if(str) delete[] str;
    88. return ret;
    89. }
    90. std::string UTF8toANSI(const char* utf8)
    91. {
    92. int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    93. wchar_t* wstr = new wchar_t[len+1];
    94. memset(wstr, 0, len+1);
    95. MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    96. len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    97. char* str = new char[len+1];
    98. memset(str, 0, len+1);
    99. WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    100. if(wstr) delete[] wstr;
    101. std::string ret = str;
    102. if(str) delete[] str;
    103. return ret;
    104. }
    105. int _tmain(int argc, _TCHAR* argv[])
    106. {
    107. std::string strSrc = "我热爱C++语言";
    108. string strUtf8 = ANSItoUTF8(strSrc.c_str());
    109. fprintf(stdout, "str: %s\n", strSrc.c_str());
    110. size_t output_length_encode = 0;
    111. char* strEncode = base64_encode((const unsigned char*)strUtf8.c_str(), strUtf8.length(), &output_length_encode);
    112. //打印编码结果
    113. std::string tmpEncode(strEncode, output_length_encode);
    114. fprintf(stdout, "encode result: %s\n", tmpEncode.c_str());
    115. size_t output_length_decode = 0;
    116. unsigned char* strDecode= base64_decode(strEncode, output_length_encode, &output_length_decode);
    117. std::string tmpDecode((char*)strDecode, output_length_decode);
    118. //打印解密结果
    119. string strDecodeUtf8 = UTF8toANSI(tmpDecode.c_str());
    120. fprintf(stdout, "str decode2: %s\n", strDecodeUtf8.c_str());
    121. free(strEncode);
    122. free(strDecode);
    123. base64_cleanup();
    124. system("pause");
    125. return 0;
    126. }

    结果:

     与在线的base64编码对照结果:

  • 相关阅读:
    力扣OJ(601-800)
    工厂模式、抽象工厂、简单工厂
    GitLab 卸载步骤 - 完全卸载
    uniapp让输入框保持聚焦状态,不会失去焦点
    Antd按钮点击完返回未点样式
    基于PTP实现主机与相机系统时钟同步功能
    汽车行驶工况||汽车行驶工况构建|||工况导入AVL Cruise(附下载)
    七天强化学习DAY1-2|(二)马尔科夫决策过程MDP|学习笔记
    【网络】计算机网络基础概念入门
    Nodejs -- Express 中间件的分类
  • 原文地址:https://blog.csdn.net/u012372584/article/details/126101074