//用一个密钥(字符串)加密另一个字符串,密钥是循环使用,加密方式为异或.现在要从密文中破解密钥来实现破解. //这里假设密钥是由小写字母构成的组合,其他情况也和这类似 #include<iostream> #include<fstream> #include<cmath> #include<string> using namespace std; int my_strlen(const char * str ){ const char *p = str; while( *p++ ) ; return( (int)(p - str - 1) ); } int function(int c1,int c2){ return c1^c2; } void JiaMi(const char* src,char * des,const char *key){ ofstream out("JiaMi.txt"); int lengthOfKey=my_strlen(key); for(int i=0;i<my_strlen(src);i++){ *(des+i)=char(function(*(src+i),*(key+i%lengthOfKey))); out<<(int)*(des+i)<<" "; } out.close(); cout<<endl; } void JieMi(const char* src,char * des,const char *key,int Length){//将src和key或运算 for(int i=0;i<Length;i++){ *(des+i)=char(function(*(src+i),*(key+i%my_strlen(key)))); } cout<<endl; } float ratio(int* src,int length){//求重合指数率的函数 int TotalOfEquals=0; int total=0; for(int i=0;i<length;i++){ for(int j=i+1;j<length;j++){ total++; if(*(src+i)==*(src+j)){ TotalOfEquals++; } } } return float((float)TotalOfEquals/(float)total); } void JieMiWithoutKey(int* src,char * des,int Length){//src是暗文,des存放目标明文,Length是暗文长度 ofstream jiemi("jiemi.txt"); //计算一下暗文的重合指数 int finalNo=0;//密码长度 int TotalOfEquals=0; int total=0; for(int i=0;i<Length;i++){ for(int j=i+1;j<Length;j++){ total++; if(*(src+i)==*(src+j)){ TotalOfEquals++; } } } cout<<"TotalOfEquals="<<TotalOfEquals<<endl; cout<<"total="<<total&