版本比较
W密码编辑器 v2.0.0 发布说明
正文
增加 34 行删除 34 行
29 行未改动30313233343435363738383940414211 行未改动54555657585859606162636465656667686910 行未改动8081828384848586878788898990919293949495969798999910010110210310 行未改动11411511611711811811912012112112212312412412512612712812913013113213213313413513612 行未改动1491501511521531531541551561561571581581591601611621631641651651661671681691701711721721731741751761771781791791801811821831841851851861871881894 行未改动1941951961971981981992002012022032042052052062072082082092102102112122132142152162172172182192202212222232242242252262272282292302312312322332342357 行未改动2432442452462472472482492502512522532542552562572572582592602612612622632642652 行未改动26826927027127227227327427527627727727827928028128228328328428528628728828929029129229229329429529667 行未改动29 行未改动string trim(const string& str);
int main() { int choice;
do { showMainMenu(); choice = getValidatedInteger("请选择操作 [1-5]: ");
switch(choice) { case 1: encryptText(); break;11 行未改动 break; default: cout << "无效选择,请重新输入!" << endl; }
if (choice != 5) { cout << "\n按回车键继续..."; cin.ignore(); cin.get(); } } while (choice != 5);
return 0;}
void showMainMenu() {10 行未改动}
void encryptText() { cout << "\n【文本加密】" << endl;
// 获取密钥 int key = getValidatedInteger("请输入加密密钥(整数): ");
cin.ignore(); // 清除输入缓冲区
// 获取要加密的文本 string text; cout << "请输入要加密的文本: "; getline(cin, text);
if (text.empty()) { cout << "输入不能为空!" << endl; return; }
// 执行加密 cout << "加密结果(数字序列): "; for (char c : text) { size_t pos = HANZI_SET.find(c);10 行未改动}
void decryptText() { cout << "\n【文本解密】" << endl;
// 获取密钥 int key = getValidatedInteger("请输入解密密钥(整数): ");
// 获取数字序列数量 int count = getValidatedInteger("请输入要解密的数据数量: ");
vector<int> numbers; cout << "请输入数字序列(用空格分隔): "; for (int i = 0; i < count; i++) { int num; cin >> num; numbers.push_back(num); }
// 执行解密 cout << "解密结果: "; for (int num : numbers) { if (num >= 0 && num < HANZI_SET.size()) {12 行未改动}
void encryptFile() { cout << "\n【文件加密】" << endl;
// 获取密钥 int key = getValidatedInteger("请输入加密密钥(整数): ");
cin.ignore(); // 清除输入缓冲区
// 获取文件路径 string sourcePath, targetPath; cout << "请输入源文件路径: "; getline(cin, sourcePath); cout << "请输入目标文件路径: "; getline(cin, targetPath);
// 打开文件 ifstream sourceFile(sourcePath); if (!sourceFile) { cout << "无法打开源文件!" << endl; return; }
ofstream targetFile(targetPath); if (!targetFile) { cout << "无法创建目标文件!" << endl; sourceFile.close(); return; }
// 读取源文件内容 stringstream buffer; buffer << sourceFile.rdbuf(); string content = buffer.str(); sourceFile.close();
// 加密内容 for (char c : content) { size_t pos = HANZI_SET.find(c); if (pos != string::npos) {4 行未改动 // 对于不在字符集中的字符,输出-1标记 targetFile << "-1 "; } }
targetFile.close(); cout << "文件加密完成!结果已保存到: " << targetPath << endl;}
void decryptFile() { cout << "\n【文件解密】" << endl;
// 获取密钥 int key = getValidatedInteger("请输入解密密钥(整数): ");
cin.ignore(); // 清除输入缓冲区
// 获取文件路径 string sourcePath, targetPath; cout << "请输入源文件路径: "; getline(cin, sourcePath); cout << "请输入目标文件路径: "; getline(cin, targetPath);
// 打开文件 ifstream sourceFile(sourcePath); if (!sourceFile) { cout << "无法打开源文件!" << endl; return; }
ofstream targetFile(targetPath); if (!targetFile) { cout << "无法创建目标文件!" << endl; sourceFile.close(); return; }
// 解密内容 int num; while (sourceFile >> num) { if (num >= 0 && num < HANZI_SET.size()) {7 行未改动 // 处理无效数字 targetFile << "#"; } }
sourceFile.close(); targetFile.close(); cout << "文件解密完成!结果已保存到: " << targetPath << endl;}
// 获取验证过的整数输入int getValidatedInteger(const string& prompt) { string input; int result;
while (true) { cout << prompt; cin >> input;
if (isAllDigits(input)) { try { result = stoi(input); break;2 行未改动 } } else { cout << "请输入有效的整数!" << endl; }
// 清除错误状态和输入缓冲区 cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); }
return result;}
// 检查字符串是否全部由数字组成bool isAllDigits(const string& str) { return all_of(str.begin(), str.end(), [](char c) { return all_of(str.begin(), str.end(), [](char c) { return isdigit(c) || (c == '-' && &c == &str[0]); // 允许负号只在开头 });}`// 去除字符串两端的空白字符string trim(const string& str) { size_t start = str.find_first_not_of(" \t\n\r"); if (start == string::npos) return "";
size_t end = str.find_last_not_of(" \t\n\r"); return str.substr(start, end - start + 1);}```67 行未改动