密碼編碼學與網絡安全(第五版)答案
阿新 • • 發佈:2018-03-15
post stream href AI mea height amp 密碼 圖片
密碼編碼學與網絡安全(第五版)答案
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<stringView Code> 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] != ‘\0‘) { 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] != ‘\0‘) { each_str[0] = str[index]; each_str[1] =str[index+1]; each_str[2] = str[index+2]; each_str[3] = ‘\0‘; 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 != ‘\0‘) { minw_num[index] = a - ‘a‘; a = s[++index]; } cout<<index<<endl; return index; } void int_to_str(char * s,int * minw_num,int len) { int index = 0; while(index < len) { s[index] = minw_num[index] + ‘a‘; index++; } } int main() { int n = 2; int A[n][n] = {{9,4},{5,7}}; char minwen[100] ="meetmeattheusualpalceattenratherthaneightoclockq"; int minw_num[100]; int miw_num[100]; int string_len = str_to_int(minwen,minw_num); int a[n],b[n]; int index = 0; while(index < string_len) { for (int j = 0; j< n; j++) { a[j] = minw_num[index + j]; } for (int j = 0; j < n;j++) { b[j] = 0; for (int k = 0; k < n;k++) b[j] =(b[j] + A[j][k]*a[k]) % 26; } for (int j = 0; j< n; j++) { minw_num[index + j] = b[j]; } index += n; } int_to_str(minwen,minw_num,string_len); cout<< minwen; return 0; }View Code
結果為:
解密求其逆矩陣即可:
*分數取模:(a/b)mod k = x ,(b,k)=1時,存在 a (mod k) = bx,即可求解x。
密碼編碼學與網絡安全(第五版)答案