密码学实验时写的代码://DsVigenere.cpp 穷举搜索法破解Vigenere #include<iostream> #include<fstream> #include<string> #include<cstdlib> //#include<stdlib.h> #include<windows.h> using namespace std; ////data structure const int pmsize=1000;//the maximum length of the plaintext //int i=0,j=0; //global variable char p[pmsize]={'\0'},k[100]={'\0'},c[pmsize]={'\0'};//p,k,c memories plainrtext,ciphertext and cipher ////preparing before begining void prepare() { cout<<endl; cout<<" "<<"穷举法破解Vigenere密码程序"<<endl; cout<<" "<<"设计者:刘懿"<<endl; cout<<" "<<"[2010.5.23]"<<endl; cout<<"==========================================================="<<endl; //judge whether entering the plaintext&cipher; char judge='Y'; cout<<"Please sure wether you have entered the plaintext&cipher" <<endl<<"with the txt form (enter Y/N):"; cin>>judge; if(judge=='N'||judge=='n') { cout<<"Please enter the plaintext&cipher with the txt form Firstly!"<<endl; exit(1); } } ////init data void init() { //read the plaintext&cipher from txt file ifstream ifs1,ifs2; if(!ifs1||!ifs2) //exit program if ifstream could not pen file { cerr<<"File could not be opened"<<endl; exit(1); } ifs1.open("plaintext1.txt");ifs1.read(p,sizeof(p));ifs1.close(); ifs2.open("ciphertext1.txt");ifs2.read(c,sizeof(c));ifs2.close(); cout<<"The plaintext is:"<<endl<<p<<endl; cout<<"The ciphertext is:"<<endl<<c<<endl; } ////Remove spaces in the arrays p&c void Rspace() { int i=0,j=0; for(i=0;p[i]!='\0';i++) { if(p[i]==' ') { for(j=i;p[j+1]!='\0';j++) p[j]=p[j+1]; p[j]='\0'; } } for(i=0;c[i]!='\0';i++) { if(c[i]==' ') { for(j=i;c[j+1]!='\0';j++) c[j]=c[j+1]; c[j]='\0 '; } } } //Crack the plaintext void Dcipher() { int number=0; for(int i=0;p[i]!='\0';i++) //dssolve the keys by brute force { for(int word='a';word<='z';word++) { if(c[i]==(word-'a'+(p[i]-'a'))%26+'a') { k[number]=word; number++; } } } for(int flag=1;flag<number;flag++) //delete the repeated cipher if(k[0]==k[flag]) k[flag]='\0'; } ////output the results and delete the plaintext&cipher in the plaintext.txt&key.txt void out() { cout<<"The key is:"<<endl<<k<<endl; //ofstream del1("plaintext.txt",ios_base::trunc); //delete the plaintext //ofstream del2("key.txt",ios_base::trunc); //delete the key } /////////////////////////////////////////////////////////// int main() { int istart,istop; prepare(); istart=GetTickCount(); init(); Rspace();//Remove spaces in the arrays Dcipher(); out(); istop=GetTickCount(); cout<<"Time spent on this program(Except for the course of preparing) is "<<istop-istart<<" ms"<<endl; system("pause"); return 0; }
//Vigenere.cpp Vigenere密码加密 #include<iostream> #include<fstream> #include<string> #include<cstdlib> #include<windows.h> using namespace std; ////data structure const int pmsize=1000;//the maximum length of the plaintext int i=0,j=0,t=0; //global variable char p[pmsize]={'\0'},k[100]={'\0'},c[pmsize]={'\0'};//p,k,c memories plainrtext,ciphertext and cipher ////preparing before begining void prepare() { cout<<endl; cout<<" "<<"Vigenere加密密码程序"<<endl; cout<<" "<<"设计者:刘懿"<<endl; cout<<" "<<"[2010.5.23]"<<endl; cout<<"========================================================="<<endl; //judge whether entering the plaintext&cipher; char judge='Y'; cout<<"Please sure wether you have entered the plaintext&cipher" <<endl<<"with the plaintext.txt&key.txt (enter Y/N):"; cin>>judge; if(judge=='N'||judge=='n') { cout<<"Please enter the plaintext&cipher with the txt form Firstly!"<<endl; exit(1); } }////init data void init() //read the plaintext&cipher from txt file { ifstream ifs1,ifs2; if(!ifs1||!ifs2) //exit program if ifstream could not pen file { cerr<<"File could not be opened"<<endl; exit(1); } ifs1.open("plaintext.txt");ifs1.read(p,sizeof(p));ifs1.close(); ifs2.open("key.txt");ifs2.read(k,sizeof(k));ifs2.close(); cout<<"The plaintext is:"<<endl<<p<<endl; } ////Remove spaces in the arrays p&k void Rspace() { for(i=0;p[i]!='\0';i++) { if(p[i]==' ') { for(j=i;p[j+1]!='\0';j++) p[j]=p[j+1]; p[j]='\0'; } } for(i=0;k[i]!='\0';i++) { if(k[i]==' ') { for(j=i;k[j+1]!='\0';j++) k[j]=k[j+1]; k[j]='\0 '; } } } //Encrypt the plaintext void Encrypt() { for(i=0;p[i]!='\0';i++) //Encrypt the plaintext c[i]=(k[i%strlen(k)]-'a'+(p[i]-'a'))%26+'a'; } ////output the results and delete the plaintext&cipher in the plaintext.txt&key.txt void out() { cout<<"The ciphertext is:"<<endl<<c<<endl; ofstream del1("plaintext.txt",ios_base::trunc); //delete the plaintext ofstream del2("key.txt",ios_base::trunc); //delete the key } /////////////////////////////////////////////////////////// int main() { int istart,istop; prepare(); istart=GetTickCount(); init(); Rspace();//Remove spaces in the arrays Encrypt(); out(); istop=GetTickCount(); cout<<"Time spent on this program(Except for the course of preparing) is "<<istop-istart<<" ms"<<endl; system("pause"); return 0; }