目录
1. 通过异或来加密解密文件
当然,下面加密方法也可作为解密方法
#include <fstream>
#include <vector>
// 获取解密内容
void get_decrypt_content(std::string _param_path)
{
std::fstream BeforeFile;
// 打开待加密
BeforeFile.open(_param_path, std::ios::in | std::ios::binary);//读|字节流
// 获取待加密文件大小
BeforeFile.seekg(0, std::ios::end);//3.1定位文件内容指针到末尾
std::streamoff size = BeforeFile.tellg(); //3.2获取大小
BeforeFile.seekg(0, std::ios::beg);//3.3 定位文件内容指针到文件头
std::vector<char> model;
for (std::streamoff i = 0; i < size; i++)
{
model.push_back(char((BeforeFile.get() ^ 0x88)));//加密或解密编码 0x88
}
std::string str_model_temp(model.begin(), model.end());
}
// 加密
void encrypte_file(std::string needEncryptFilePath, std::string saveFilePath)
{
// 待加密文件的流对象
std::fstream BeforeFile, AfterFile;
// 打开待加密
BeforeFile.open(needEncryptFilePath, std::ios::in | std::ios::binary);//读|字节流
// 打开待解密后文件
AfterFile.open(saveFilePath, std::ios::out | std::ios::binary);
// 获取待加密文件大小
BeforeFile.seekg(0, std::ios::end);//3.1定位文件内容指针到末尾
std::streamoff size = BeforeFile.tellg(); //3.2获取大小
BeforeFile.seekg(0, std::ios::beg);//3.3 定位文件内容指针到文件头
for (std::streamoff i = 0; i < size; i++)
{
//把BeforeFile的内容处理后放入AfterFile中
AfterFile.put(BeforeFile.get() ^ 0x88);//加密或解密编码 0x88
}
// 关闭两个文件
BeforeFile.close();
AfterFile.close();
}