资源简介
有关信息安全的凯撒密码c++代码,其中文件输入和输出的地址可能需要大家重新编写一下
代码片段和文件信息
#include
#include
#include
using namespace std;
#define infinite 9999999
void encryption(stringint);
void decryption(stringintint);
void getFrequency(double*string);
void crack(string);
int main()
{
int key;
string FilePath;
int option ; //选择项
while(1)
{
cout<<“please select option number:“< cout<<“1.encryption“< cout<<“2.decryption“< cout<<“3.crack“< cout<<“4.exit“< cin >> option;
if(option==4) break;
switch (option)
{
case 1:
cout<<“please enter the file path:“< cin>>FilePath;
cout<<“please enter the key:“< cin>>key;
encryption(FilePathkey);
break;
case 2:
cout<<“please enter the file path:“< cin>>FilePath;
cout<<“please enter the key:“< cin>>key;
decryption(FilePathkey0); //0表示解密 1表示破解
break;
case 3:
cout<<“please enter the file path:“< cin>>FilePath;
crack(FilePath);
break;
}
}
}
void encryption(string filepathint key)
{
char character;
ifstream inputFile(filepathios::in);
ofstream encrpFile(“encryption\\encryption.txt“ios::out);
//取单个字符分别加密
while(!inputFile.eof())
{
//inputFile>>character;
inputFile.get(character);
if(character>=‘a‘&&character<=‘z‘)
{
character=character+key<=‘z‘?character+key:character-26+key; //减去key小于a的aski码值+26
}
else if(character>=‘A‘&&character<=‘Z‘)
{
character=character-key>=‘Z‘?character-key:character-26+key;
}
encrpFile< }
inputFile.close();
encrpFile.close();
}
void decryption(string filepathint keyint option)
{
char character;
string decryPath;
ifstream inputFile(filepathios::in);
if(option==0) //解密
{
decryPath=“decryption\\decrption.txt“;
}else if(option==1)
{
decryPath=“crack\\crack.txt“;
}
ofstream decryFile(decryPathios::out);
//取单个字符分别加密
while(!inputFile.eof())
{
inputFile>>character;
if(character>=‘a‘&&character<=‘z‘)
{
character=character-key>=‘a‘?character-key:character+26-key; //减去key小于a的aski码值+26
}
else if(character>=‘A‘&&character<=‘Z‘)
{
character=character-key>=‘A‘?character-key:character+26-key;
}
decryFile< }
inputFile.close();
decryFile.close();
}
void crack(string filepath)
{
int key;
double frequency[26]={0};
long double temp;
double Chi_squared=infinite;
double expect[26]={0.081670.014920.027820.042530.127020.022280.020150.060940.069660.001530.00772
0.040250.024060.067490.075070.019290.000950.059870.063270.090560.027580.009780.023610.001500.019740.00074};
getFrequency(frequencyfilepath);
for( int j=0;j<26;j++)
{
temp=0;
for(int i=0;i<26;i++) //卡方校验
{
temp+=(frequency[(i+j)%26]-expect[i])*(frequency[(i+j)%26]-expect[i])/expect[i];
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3226 2017-10-24 17:41 CaesarCipher\CaesarCipher\CaesarCipher.vcxproj
文件 942 2017-10-24 17:41 CaesarCipher\CaesarCipher\CaesarCipher.vcxproj.filters
文件 143 2017-10-24 16:14 CaesarCipher\CaesarCipher\CaesarCipher.vcxproj.user
文件 265 2017-10-31 16:31 CaesarCipher\CaesarCipher\crack\crack.txt
文件 381 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\CaesarCipher.exe.intermediate.manifest
文件 67 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\CaesarCipher.lastbuildstate
文件 2009 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\CaesarCipher.log
文件 713 2017-10-25 20:39 CaesarCipher\CaesarCipher\Debug\CaesarCipher.vcxprojResolveAssemblyReference.cache
文件 0 2017-10-25 20:39 CaesarCipher\CaesarCipher\Debug\CaesarCipher.write.1.tlog
文件 560 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\cl.command.1.tlog
文件 3954 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\CL.read.1.tlog
文件 336 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\CL.write.1.tlog
文件 1188 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\li
文件 2480 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\li
文件 604 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\li
文件 254073 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\main.obj
文件 468 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\mt.command.1.tlog
文件 898 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\mt.read.1.tlog
文件 282 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\mt.write.1.tlog
文件 207872 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\vc100.idb
文件 249856 2017-10-31 16:38 CaesarCipher\CaesarCipher\Debug\vc100.pdb
文件 265 2017-10-31 16:30 CaesarCipher\CaesarCipher\decryption\decrption.txt
文件 340 2017-10-31 16:38 CaesarCipher\CaesarCipher\encryption\encryption.txt
文件 3796 2017-10-31 16:38 CaesarCipher\CaesarCipher\main.cpp
文件 6377472 2017-11-14 17:53 CaesarCipher\CaesarCipher.sdf
文件 903 2017-10-24 16:14 CaesarCipher\CaesarCipher.sln
..A..H. 15872 2017-11-14 17:53 CaesarCipher\CaesarCipher.suo
文件 98816 2017-10-31 16:38 CaesarCipher\Debug\CaesarCipher.exe
文件 666396 2017-10-31 16:38 CaesarCipher\Debug\CaesarCipher.ilk
文件 756736 2017-10-31 16:38 CaesarCipher\Debug\CaesarCipher.pdb
............此处省略13个文件信息
评论
共有 条评论