密码编码学与网络安全(第五版)答案
https://wenku.baidu.com/view/283a5dbb5727a5e9856a61ff.html
2.4题:
通过如下代码分别统计一个字符的频率和三个字符的频率,"8"——"e",“;48”——“the”,英文字母的相对使用频率,猜测频率比较高的依此为),t,*,5,分别对应s,o,n,a;由此破出明文。

#include<iostream> #include<map> #include<vector> #include<algorithm> #include<string> using namespace std; int cmp(const pair<string, int>& x, const pair<string, int>& y) { return x.second > y.second; } void sortMapByValue(map<string, int>& tMap, vector<pair<string, int> >& tVector) { for (map<string, int>::iterator curr = tMap.begin(); curr != tMap.end(); curr++) { tVector.push_back(make_pair(curr->first, curr->second)); } sort(tVector.begin(), tVector.end(), cmp); } int cmp1(const pair<char, int>& x, const pair<char, int>& y) { return x.second > y.second; } void sortMapByValue(map<char, int>& tMap, vector<pair<char, int> >& tVector) { for (map<char, int>::iterator curr = tMap.begin(); curr != tMap.end(); curr++) { tVector.push_back(make_pair(curr->first, curr->second)); } sort(tVector.begin(), tVector.end(), cmp1); } void char_pl() { map<char,int> mapstr; char* str = "53ttp305))6*;4826)4t.)4t);8O6*;48p8q60))85;;]8*;:t*8p83(88)5*p;46(;88*96*?;8)*t(;485);5*p2:*t(;4956*2(5*-4)8q8*;4069285);)6p8)4tt;1(t9;48081;8:8t1;48p85;4)485p528806*81(t9;48;(88;4(t?34;48)4t;161;:188;t?;"; int index = 0; while(str[index] != ' ') { if(mapstr.find(str[index]) ==mapstr.end()) mapstr[str[index]] =0; mapstr[str[index]] ++; index++; } vector<pair<char,int> > tVector; sortMapByValue(mapstr,tVector); for(int i=0; i<tVector.size(); i++) { cout<<tVector[i].first<<": "<<tVector[i].second<<endl; } } void zimupl() { map<string,int> mapstr; char* str = "53ttp305))6*;4826)4t.)4t);8O6*;48p8q60))85;;]8*;:t*8p83(88)5*p;46(;88*96*?;8)*t(;485);5*p2:*t(;4956*2(5*-4)8q8*;4069285);)6p8)4tt;1(t9;48081;8:8t1;48p85;4)485p528806*81(t9;48;(88;4(t?34;48)4t;161;:188;t?;"; int index = 0; char each_str[4]; string e_s; while(str[index] != ' ') { each_str[0] = str[index]; each_str[1] =str[index+1]; each_str[2] = str[index+2]; each_str[3] = ' '; e_s = each_str; if(mapstr.find(e_s) ==mapstr.end()) mapstr[e_s] =0; mapstr[e_s] ++; index++; } vector<pair<string,int> > tVector; sortMapByValue(mapstr,tVector); for(int i=0; i<tVector.size(); i++) { cout<<tVector[i].first<<": "<<tVector[i].second<<endl; } } int main() { char_pl(); zimupl(); return 0; }
英文字母的相对使用频率
破解出的明文
2.14:
根据上述结果代码如下:不够长补了一个p

#include<cstdio> #include<iostream> #include<string> using namespace std; int str_to_int(char * s,int * minw_num) { char a = s[0]; int index = 0; while(a != '