使用mfc對文本數據進行加密的方法
mfc代碼如下:
#include "stdafx.h"
#include "EncodeAndDecode.h"
#include "afxdialogex.h"
EncodeAndDecode::EncodeAndDecode()
{
}
EncodeAndDecode::~EncodeAndDecode()
{
}
void EncodeAndDecode::EncodeFile()
{
//讀取配置文件
CString fPath("./data.txt");
CFile openFile;
bool bFile = openFile.Open(fPath, CFile::modeRead);
//同根目錄下判斷是否打開
if (!bFile)
{
MessageBox(NULL,_T("系統文件丟失請聯系管理員"), _T("錯誤") ,MB_ICONINFORMATION);
exit(0);
}
DWORD openSize = (DWORD)openFile.GetLength();
char* buf = new char[openSize + 1];
memset(buf, 0, sizeof(char) * (openSize + 1));
openFile.Read(buf, openSize);
openFile.Close();
//加密算法
for (unsigned int i = 0; i < openSize; i++)
{
buf[i] = 255 - buf[i];
}
//寫入加密文件
CString wPath("./moon.xie");
CFile writeFile;
writeFile.Open(wPath, CFile::modeWrite | CFile::modeCreate);
writeFile.Write(buf, openSize);
writeFile.Close();
//MessageBox(NULL, _T("已加密"), _T("成功"), MB_ICONINFORMATION);
}