赞
踩
核心的转换函数:
- std::string CMyBase64mfc::base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len)
- {
- std::string ret;
- int i = 0;
- int j = 0;
- unsigned char char_array_3[3];
- unsigned char char_array_4[4];
-
- while (in_len--) {
- char_array_3[i++] = *(bytes_to_encode++);
- if (i == 3) {
- char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
- char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
- char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
- char_array_4[3] = char_array_3[2] & 0x3f;
-
- for(i = 0; (i <4) ; i++)
- ret += base64_chars[char_array_4[i]];
- i = 0;
- }
- }
-
- if (i)
- {
- for(j = i; j < 3; j++)
- char_array_3[j] = '\0';
-
- char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
- char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
- char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
- char_array_4[3] = char_array_3[2] & 0x3f;
-
- for (j = 0; (j < i + 1); j++)
- ret += base64_chars[char_array_4[j]];
-
- while((i++ < 3))
- ret += '=';
-
- }
-
- return ret;
- }
-
- std::string CMyBase64mfc::base64_decode(std::string const& encoded_string)
- {
- int in_len = encoded_string.size();
- int i = 0;
- int j = 0;
- int in_ = 0;
- unsigned char char_array_4[4], char_array_3[3];
- std::string ret;
-
- while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
- char_array_4[i++] = encoded_string[in_]; in_++;
- if (i ==4) {
- for (i = 0; i <4; i++)
- char_array_4[i] = base64_chars.find(char_array_4[i]);
-
- char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
- char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
- char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
-
- for (i = 0; (i < 3); i++)
- ret += char_array_3[i];
- i = 0;
- }
- }
-
- if (i) {
- for (j = i; j <4; j++)
- char_array_4[j] = 0;
-
- for (j = 0; j <4; j++)
- char_array_4[j] = base64_chars.find(char_array_4[j]);
-
- char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
- char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
- char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
-
- for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
- }
-
- return ret;
- }
下面是封装的加解密文件的函数。方法就是先打开文件,读取所有字符。
注意这里是按行读取,并且舍弃了换行符。因此读取每一行后要加上换行符再组合起来,base64转换后最后写入文件。
- bool CMyBase64mfc::Decode(CString cstrPath)
- {
- //读取
- CStdioFile file;
- BOOL flag = file.Open(cstrPath, CFile::modeRead);
- CString ;
- CString text, strline;
- if(flag == FALSE)
- {
- text.Format("读取文件%s失败!", cstrPath);
- UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
- MessageBoxTimeout(NULL, text, _T("提示"), uiFlags, 0, 1000);
-
- return false;
- }
-
- file.Seek(0, CFile::begin);
- CString content;
- while(file.ReadString(strline))
- {
- strline += "\n";//加换行符
- content += strline;
- }
-
- file.Close();
-
- //解密
- int iLength = 0;
- std::string base64Content = base64_decode((LPCSTR)(CStringA)content);
-
- //保存
- if (file.Open(cstrPath, CFile::modeCreate | CFile::modeWrite))
- {
- //因为txt中的内容被清空了,所以用begin or end 都可以
- file.Seek(0, CFile::begin);
- file.WriteString(base64Content.c_str());
- }
- else
- {
- UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
- MessageBoxTimeout(NULL, _T("文件解密失败!请手动解密!"), _T("提示"), uiFlags, 0, 1000);
- file.Close();
- return true;
- }
-
- file.Close();
- return true;
- }
-
- bool CMyBase64mfc::Encode(CString cstrPath)
- {
- //读取
- CStdioFile file;
- BOOL flag = file.Open(cstrPath, CFile::modeRead);
- CString ;
- CString text, strline;
- if(flag == FALSE)
- {
- text.Format("读取文件%s失败!", cstrPath);
- UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
- MessageBoxTimeout(NULL, text, _T("提示"), uiFlags, 0, 1000);
-
- return false;
- }
-
- file.Seek(0, CFile::begin);
- CString content;
- while(file.ReadString(strline))
- {
- strline += "\n";
- content += strline;
- }
-
- file.Close();
-
- //加密
- std::string base64Content = base64_encode((unsigned char*)content.GetBuffer(content.GetLength()), content.GetLength());
-
-
- //保存
- if (file.Open(cstrPath, CFile::modeCreate | CFile::modeWrite))
- {
- //因为txt中的内容被清空了,所以用begin or end 都可以
- file.Seek(0, CFile::begin);
- file.WriteString(base64Content.c_str());
- }
- else
- {
- UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
- MessageBoxTimeout(NULL, _T("文件加密失败!请手动加密!"), _T("提示"), uiFlags, 0, 1000);
- file.Close();
- return true;
- }
-
- file.Close();
- return true;
- }
测试:
- void CMyBase64Dlg::OnBnClickedOk()
- {
- // TODO: 在此添加控件通知处理程序代码
- CMyBase64mfc b;
- b.Decode("E:\\myVCProject\\MyBase64\\ReadMe.txt");
-
- }
-
-
- void CMyBase64Dlg::OnBnClickedCancel()
- {
- // TODO: 在此添加控件通知处理程序代码
- CMyBase64mfc b;
- b.Encode("E:\\myVCProject\\MyBase64\\ReadMe.txt");
- }
QT下Base64加解密请看
https://blog.csdn.net/qq_24282081/article/details/94906090
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。