当前位置:   article > 正文

纯c++实现Base64加解密,MFC封装成加解密文件_mfc base64

mfc base64

核心的转换函数:

  1. std::string CMyBase64mfc::base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len)
  2. {
  3. std::string ret;
  4. int i = 0;
  5. int j = 0;
  6. unsigned char char_array_3[3];
  7. unsigned char char_array_4[4];
  8. while (in_len--) {
  9. char_array_3[i++] = *(bytes_to_encode++);
  10. if (i == 3) {
  11. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  12. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  13. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  14. char_array_4[3] = char_array_3[2] & 0x3f;
  15. for(i = 0; (i <4) ; i++)
  16. ret += base64_chars[char_array_4[i]];
  17. i = 0;
  18. }
  19. }
  20. if (i)
  21. {
  22. for(j = i; j < 3; j++)
  23. char_array_3[j] = '\0';
  24. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  25. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  26. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  27. char_array_4[3] = char_array_3[2] & 0x3f;
  28. for (j = 0; (j < i + 1); j++)
  29. ret += base64_chars[char_array_4[j]];
  30. while((i++ < 3))
  31. ret += '=';
  32. }
  33. return ret;
  34. }
  35. std::string CMyBase64mfc::base64_decode(std::string const& encoded_string)
  36. {
  37. int in_len = encoded_string.size();
  38. int i = 0;
  39. int j = 0;
  40. int in_ = 0;
  41. unsigned char char_array_4[4], char_array_3[3];
  42. std::string ret;
  43. while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  44. char_array_4[i++] = encoded_string[in_]; in_++;
  45. if (i ==4) {
  46. for (i = 0; i <4; i++)
  47. char_array_4[i] = base64_chars.find(char_array_4[i]);
  48. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  49. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  50. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  51. for (i = 0; (i < 3); i++)
  52. ret += char_array_3[i];
  53. i = 0;
  54. }
  55. }
  56. if (i) {
  57. for (j = i; j <4; j++)
  58. char_array_4[j] = 0;
  59. for (j = 0; j <4; j++)
  60. char_array_4[j] = base64_chars.find(char_array_4[j]);
  61. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  62. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  63. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  64. for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  65. }
  66. return ret;
  67. }

下面是封装的加解密文件的函数。方法就是先打开文件,读取所有字符。

注意这里是按行读取,并且舍弃了换行符。因此读取每一行后要加上换行符再组合起来,base64转换后最后写入文件。

  1. bool CMyBase64mfc::Decode(CString cstrPath)
  2. {
  3. //读取
  4. CStdioFile file;
  5. BOOL flag = file.Open(cstrPath, CFile::modeRead);
  6. CString ;
  7. CString text, strline;
  8. if(flag == FALSE)
  9. {
  10. text.Format("读取文件%s失败!", cstrPath);
  11. UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
  12. MessageBoxTimeout(NULL, text, _T("提示"), uiFlags, 0, 1000);
  13. return false;
  14. }
  15. file.Seek(0, CFile::begin);
  16. CString content;
  17. while(file.ReadString(strline))
  18. {
  19. strline += "\n";//加换行符
  20. content += strline;
  21. }
  22. file.Close();
  23. //解密
  24. int iLength = 0;
  25. std::string base64Content = base64_decode((LPCSTR)(CStringA)content);
  26. //保存
  27. if (file.Open(cstrPath, CFile::modeCreate | CFile::modeWrite))
  28. {
  29. //因为txt中的内容被清空了,所以用begin or end 都可以
  30. file.Seek(0, CFile::begin);
  31. file.WriteString(base64Content.c_str());
  32. }
  33. else
  34. {
  35. UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
  36. MessageBoxTimeout(NULL, _T("文件解密失败!请手动解密!"), _T("提示"), uiFlags, 0, 1000);
  37. file.Close();
  38. return true;
  39. }
  40. file.Close();
  41. return true;
  42. }
  43. bool CMyBase64mfc::Encode(CString cstrPath)
  44. {
  45. //读取
  46. CStdioFile file;
  47. BOOL flag = file.Open(cstrPath, CFile::modeRead);
  48. CString ;
  49. CString text, strline;
  50. if(flag == FALSE)
  51. {
  52. text.Format("读取文件%s失败!", cstrPath);
  53. UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
  54. MessageBoxTimeout(NULL, text, _T("提示"), uiFlags, 0, 1000);
  55. return false;
  56. }
  57. file.Seek(0, CFile::begin);
  58. CString content;
  59. while(file.ReadString(strline))
  60. {
  61. strline += "\n";
  62. content += strline;
  63. }
  64. file.Close();
  65. //加密
  66. std::string base64Content = base64_encode((unsigned char*)content.GetBuffer(content.GetLength()), content.GetLength());
  67. //保存
  68. if (file.Open(cstrPath, CFile::modeCreate | CFile::modeWrite))
  69. {
  70. //因为txt中的内容被清空了,所以用begin or end 都可以
  71. file.Seek(0, CFile::begin);
  72. file.WriteString(base64Content.c_str());
  73. }
  74. else
  75. {
  76. UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
  77. MessageBoxTimeout(NULL, _T("文件加密失败!请手动加密!"), _T("提示"), uiFlags, 0, 1000);
  78. file.Close();
  79. return true;
  80. }
  81. file.Close();
  82. return true;
  83. }

测试:

  1. void CMyBase64Dlg::OnBnClickedOk()
  2. {
  3. // TODO: 在此添加控件通知处理程序代码
  4. CMyBase64mfc b;
  5. b.Decode("E:\\myVCProject\\MyBase64\\ReadMe.txt");
  6. }
  7. void CMyBase64Dlg::OnBnClickedCancel()
  8. {
  9. // TODO: 在此添加控件通知处理程序代码
  10. CMyBase64mfc b;
  11. b.Encode("E:\\myVCProject\\MyBase64\\ReadMe.txt");
  12. }

QT下Base64加解密请看

https://blog.csdn.net/qq_24282081/article/details/94906090

 

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

闽ICP备14008679号