编写程序进行简易的加密解密

黄锭宪 黄锭宪 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
已知文件input.txt,逐一读入文件中的字符,将其中的字符的ASCII码+数字(1~9),再逐一写入到code.txt中。 已知文件code.txt,将其解码后输出到output.txt中。 对比input.txt和output.txt(提示:可使用system(“fc input.txt output.txt”)) 要求有如下菜单 ------------------------------ 菜单 ------------------------------ 1. 退出 2. 加密文件 3. 解密文件 4. 比较文件 ----------------------------- 本人不会将其中的字符的ASCII码+数字(1~9)这部分,求大神,还有解密该怎么解,下面是本人写的代码 #include<iostream> #include<fstream> #include<string> using namespace std; void Menu1(); char ch; char filename[40]; char infilename[40]; int number1; int number2; void Menu1() { cout << "-----------------------------" << endl; cout << " 菜单" << endl; cout << "-----------------------------" << endl; cout << "\t0. 退出" << endl; cout << "\t1. 加密文件" << endl; cout << "\t2. 解密文件" << endl; cout << "\t3. 比较文件" << endl; cout << "-----------------------------" << endl; cout << "请输入选择:(0~3)" << endl; } void Encrytfile(); void Encrytfile() { cout << "请输入你要加密的文件名" << endl; cin >> infilename; cout << "请输入密码(1~9):" << endl; cin >> number1; ifstream infile; infile.open(infilename, ios::in); if (infile.fail()) { cout << "无法打开文件" << endl; return; } ofstream outfile; outfile.open("code.txt", ios::out); if (outfile.fail()) { cout << "无法打开文件" << endl; return; } while (infile.get(ch)) outfile << ch + number1; infile.close(); outfile.close(); } void DeclassifiedFile(); void DeclassifiedFile() { cout << "请输入你要解密的文件名" << endl; cin >> filename; cout << "请输入密码(1~9):" << endl; cin >> number2; ifstream in; in.open(filename, ios::in); if (in.fail()) { cout << "无法打开文件" << endl; return; } ofstream out; out.open("output.txt"); if (out.fail()) { cout << "无法打开文件" << endl; return; } while (in.get(ch)) out << ch - number2; in.close(); out.close(); } void Comparefile(); void Comparefile() { system("fc input.txt output.txt"); } int main() { int userchoice1; do { Menu1(); cin >> userchoice1; switch (userchoice1) { case 0: return 0; case 1: Encrytfile(); case 2: DeclassifiedFile(); case 3: Comparefile(); } } while (userchoice1 != 0); } 但是解密出来和原来不一样
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
黄锭宪
C++ 语言 65,214 成员 250,514 内容