编写程序进行简易的加密解密
黄锭宪 2019-05-18 02:15:33 已知文件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);
}
但是解密出来和原来不一样