#include<fstream> // 文件流
#include<iostream> // 标准流
#include<string> // 字符串对象
using namespace std; // 使用std名字空间
void main()
{
string choose;
L1: cout<<"1、encrytion"<<endl<<"2、decrytion"<<endl<<"3、quit"<<endl<<"what do you want to do?"<<endl;
cin>>choose;
if(choose=="1")//加密
{
//新建一个加密文件在d/encrytion.txt
ifstream inFile;
inFile.open("d://test.txt"); // 打开文件
string str; // 行字符串缓存
if(inFile.is_open())
{ // 如果创建成功,填入内容
//在输入内容之前,首先输入加密密钥(介于1和25之间的一个整数)
int Ek;
cout<<"please put in your encrytion key(the number between 1 and 25):"<<endl;
cin>>Ek;
cout<<endl;
if(Ek>=1&&Ek<=25)
{
ofstream outFile;
outFile.open("d://encrytion.txt"); // 创建文件
if(outFile.is_open())
{ // 若成功打开文件
while(!inFile.eof())
{ // 若未到文件结束
getline(inFile, str, '\n'); // 读取一行内容,并存入缓存str中,'\n'表示一行结束的回车符
//cout << str << endl; // 把缓存内容输出到屏幕
for(int i=0;str[i]!=NULL;i++)
{
if(str[i]>=65&&str[i]<=90)
str[i]=(str[i]+Ek-65)%26+65;
else if(str[i]>=97&&str[i]<=122)
str[i]=(str[i]-97+Ek)%26+97;
else str[i]=str[i];
outFile<<str[i];
}
outFile<<endl;
}
cout<<"The encrytion is sucessd,"<<endl<<" the encrytion file is in d:// ,called encrytion.txt!"<<endl;
}
else
cout<<"we coun't built the encrytion.txt"<<endl;
outFile.close(); // 关闭文件
}
else cout<<"The number you put is wrong!"<<endl;
}
else
cout<<"we can't find the file called test.txt in d:/"<<endl;
inFile.close();
}
else if(choose=="2")//解密
{
int temp=0;
ifstream inFile;
inFile.open("d://test.txt");
if(inFile.is_open())
{ string str;
int a[27]={0};//0--25用来存储a(A)-z(Z)的个数,26用来存储总字符的个数。
while(!inFile.eof())
{
getline(inFile,str,'\n');
for(int i=0;str[i]!=NULL;i++)
{
switch(str[i])
{
case 'a':{a[0]++;a[26]++;break;} case 'A':{a[0]++;a[26]++;break;}
case 'b':{a[1]++;a[26]++;break;} case 'B':{a[1]++;a[26]++;break;}
case 'c':{a[2]++;a[26]++;break;} case 'C':{a[2]++;a[26]++;break;}
case 'd':{a[3]++;a[26]++;break;} case 'D':{a[3]++;a[26]++;break;}
case 'e':{a[4]++;a[26]++;break;} case 'E':{a[4]++;a[26]++;break;}
case 'f':{a[5]++;a[26]++;break;} case 'F':{a[5]++;a[26]++;break;}
case 'g':{a[6]++;a[26]++;break;} case 'G':{a[6]++;a[26]++;break;}
case 'h':{a[7]++;a[26]++;break;} case 'H':{a[7]++;a[26]++;break;}
case 'i':{a[8]++;a[26]++;break;} case 'I':{a[8]++;a[26]++;break;}
case 'j':{a[9]++;a[26]++;break;} case 'J':{a[9]++;a[26]++;break;}
case 'k':{a[10]++;a[26]++;break;} case 'K':{a[10]++;a[26]++;break;}
case 'l':{a[11]++;a[26]++;break;} case 'L':{a[11]++;a[26]++;break;}
case 'm':{a[12]++;a[26]++;break;} case 'M':{a[12]++;a[26]++;break;}
case 'n':{a[13]++;a[26]++;break;} case 'N':{a[13]++;a[26]++;break;}
case 'o':{a[14]++;a[26]++;break;} case 'O':{a[14]++;a[26]++;break;}
case 'p':{a[15]++;a[26]++;break;} case 'P':{a[15]++;a[26]++;break;}
case 'q':{a[16]++;a[26]++;break;} case 'Q':{a[16]++;a[26]++;break;}
case 'r':{a[17]++;a[26]++;break;} case 'R':{a[17]++;a[26]++;break;}
case 's':{a[18]++;a[26]++;break;} case 'S':{a[18]++;a[26]++;break;}
case 't':{a[19]++;a[26]++;break;} case 'T':{a[19]++;a[26]++;break;}
case 'u':{a[20]++;a[26]++;break;} case 'U':{a[20]++;a[26]++;break;}
case 'v':{a[21]++;a[26]++;break;} case 'V':{a[21]++;a[26]++;break;}
case 'w':{a[22]++;a[26]++;break;} case 'W':{a[22]++;a[26]++;break;}
case 'x':{a[23]++;a[26]++;break;} case 'X':{a[23]++;a[26]++;break;}
case 'y':{a[24]++;a[26]++;break;} case 'Y':{a[24]++;a[26]++;break;}
case 'z':{a[25]++;a[26]++;break;} case 'Z':{a[25]++;a[26]++;break;}
// default:a[26]++;
}
}
}//全部数据统计结束!
for(int i=1;i<26;i++)
if(a[i]>a[temp])
temp=i;
cout<<"文件中各个字符出现的概率:"<<endl;
for(i=0;i<26;i++)
cout<<char(65+i)<<":"<<a[i]*0.1/a[26]*10<<'\t';
cout<<endl;
if(a[temp]>a[26]*0.100&&a[temp]<a[26]*0.140&& //e
a[(temp+22)%26]>0.06*a[26]&&a[(temp+22)%26]<0.09*a[26]&& //a
a[(temp+10)%26]>0.06*a[26]&&a[(temp+10)%26]<0.09*a[26]&& //o
a[(temp+4)%26]>0.06*a[26]&&a[(temp+4)%26]<0.09*a[26]&& //i
a[(temp+1)%26]>0.015*a[26]&&a[(temp+1)%26]<0.028*a[26]&& //f
a[(temp+2)%26]>0.015*a[26]&&a[(temp+2)%26]<0.028*a[26]&& //g
a[(temp+20)%26]>0.015*a[26]&&a[(temp+20)%26]<0.028*a[26]&& //y
a[(temp+19)%26]<0.01*a[26]&& //x
a[(temp+12)%26]<0.01*a[26]&& //q
a[(temp+21)%26]<0.01*a[26] //z
)//符合其他概率要求,则密钥确定
{
temp=(temp-4+26)%26;//计算密钥
cout<<"文件已解密"<<endl<<"解密密钥: "<<temp<<endl;
}
else cout<<"内容太少,无法解密!"<<endl;
cout<<"文件中共出现英语字母个数:"<<a[26]<<endl;
}
else cout<<"we can't find the file called test.txt in d:/"<<endl;
inFile.close();
if(temp!=0)
{
ifstream inFile2;
inFile2.open("d://test.txt");
if(inFile2.is_open())
{
string str;
ofstream outFile;
outFile.open("d://decrytion.txt"); // 创建文件
if(outFile.is_open())
{
while(!inFile2.eof())//inFile文件重新打开!
{
getline(inFile2,str,'\n');
for(int i=0;str[i]!=NULL;i++)
{
if(str[i]>=65&&str[i]<=90)
str[i]=(str[i]-65-temp+26)%26+65;
else if(str[i]>=97&&str[i]<=122)
str[i]=(str[i]-97-temp+26)%26+97;
else str[i]=str[i];
outFile<<str[i];
}
outFile<<endl;
}
cout<<"解密成功,解密文件decrytion.txt放在D盘下"<<endl;
inFile.close();
}
else cout<<"we can't build the file called decrytion.txt in d://!"<<endl;
}
else return;
}
else
return;
}
else if(choose=="3")//退出
{
return;
}
else
{
cout<<"you put the wrong number!"<<endl;
goto L1;
}
}
- 1
- 2
前往页