PS:代码默认解压密码是password,可自行在第122行修改
上代码:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <limits> // 引入limits头文件以使用std::numeric_limits
using namespace std;
// 使用密钥字符串进行 XOR 加密/解密
void xorEncryptDecrypt(vector<unsigned char>& data, const string& key) {
size_t keyLen = key.length();
size_t keyIndex = 0; // 用来指示密钥字符的位置
for (auto& byte : data) {
byte ^= key[keyIndex]; // XOR 当前字节与密钥字符
keyIndex = (keyIndex + 1) % keyLen; // 密钥字符循环使用
}
}
// 使用简单的行程长度编码 (RLE) 进行压缩
vector<unsigned char> compressRLE(const vector<unsigned char>& input) {
vector<unsigned char> compressed;
if (input.empty()) return compressed;
unsigned char currentChar = input[0];
int count = 1;
// 遍历输入数据,进行 RLE 压缩
for (size_t i = 1; i < input.size(); ++i) {
if (input[i] == currentChar && count < 255) {
++count; // 继续计数
}
else {
compressed.push_back(currentChar);
compressed.push_back(static_cast<unsigned char>(count)); // 存储字符和它的重复次数
currentChar = input[i];
count = 1;
}
}
// 处理最后一组字符
compressed.push_back(currentChar);
compressed.push_back(static_cast<unsigned char>(count));
return compressed;
}
// 解压 RLE 数据
vector<unsigned char> decompressRLE(const vector<unsigned char>& compressed) {
vector<unsigned char> decompressed;
for (size_t i = 0; i < compressed.size(); i += 2) {
unsigned char currentChar = compressed[i];
int count = compressed[i + 1];
for (int j = 0; j < count; ++j) {
decompressed.push_back(currentChar);
}
}
return decompressed;
}
// 将文件内容读取到 vector
vector<unsigned char> readFile(const string& fileName) {
ifstream inFile(fileName, ios::binary);
if (!inFile) {
cerr << "无法打开文件 " << fileName << endl;
return {};
}
return vector<unsigned char>((istreambuf_iterator<char>(inFile)), istreambuf_iterator<char>());
}
// 将数据写入文件
void writeFile(const string& fileName, const vector<unsigned char>& data) {
ofstream outFile(fileName, ios::binary);
if (!outFile) {
cerr << "无法写入文件 " << fileName << endl;
return;
}
outFile.write(reinterpret_cast<const char*>(data.data()), data.size());
}
int main() {
string inputFileName;
string outputFileName;
string password;
int choice;
cout << "选择操作:1. 压缩并加密文件 2. 解压并解密文件" << endl;
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除输入缓冲区
if (choice == 1) {
cout << "输入文件名:";
getline(cin, inputFileName);
cout << "输出文件名:";
getline(cin, outputFileName);
// 读取输入文件
vector<unsigned char> inputData = readFile(inputFileName);
// 压缩文件
vector<unsigned char> compressedData = compressRLE(inputData);
// 使用 XOR 加密压缩文件
string encryptionKey = "kaihan122525"; // 你可以自定义更复杂的密钥
xorEncryptDecrypt(compressedData, encryptionKey);
// 写入加密后的压缩数据到文件
writeFile(outputFileName, compressedData);
cout << "文件已成功压缩并加密!" << endl;
}
else if (choice == 2) {
cout << "输入加密文件名:";
getline(cin, inputFileName);
cout << "输出解压文件名:";
getline(cin, outputFileName);
cout << "请输入解压密码:";
getline(cin, password);
if (password == "password") {
// 读取加密后的压缩文件
vector<unsigned char> encryptedData = readFile(inputFileName);
// 解密文件
xorEncryptDecrypt(encryptedData, password);
// 解压缩文件
vector<unsigned char> decompressedData = decompressRLE(encryptedData);
// 写入解压后的数据到文件
writeFile(outputFileName, decompressedData);
cout << "文件已成功解压!" << endl;
}
else {
cout << "密码错误!" << endl;
}
}
else {
cout << "无效的选择!" << endl;
}
return 0;
}