密码编码学与网络安全(第五版)答案

密码编码学与网络安全(第五版)答案

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;
}
View Code

英文字母的相对使用频率

  

破解出的明文

  

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 != '')
    {
        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。

原文地址:https://www.cnblogs.com/yutingmoran/p/8573058.html