设明文
其中
参考密码以下web
ktbueluegvitnthuexmonveggmrcgxptlyhhjaogchoemqchpdnetxupbqntietiabpsmaoncnwvoutiugtagmmqsxtvxaoniiogtagmbpsmtuvvihpstpdvcrxhokvhxotawswquunewcgxptlcrxtevtubvewcnwwsxfsnptswtagakvoyyak
若是密文中出现两个相同的密文片断,那么它们对应的明文片断极有多是相同的,则密码片断极可能是这些距离的最大公因子。ide
如下C++代码输出相同长度为3的代码片断之间距离。svg
/* birdy&C 17.3.11 多表加密 */
#include <iostream>
#include <algorithm>
#include <functional>
#include <set>
#include <iterator>
#include <vector>
#include<string>
using namespace std;
int main(void)
{
string code;
code = "ktbueluegvitnthuexmonveggmrcgxptlyhhjaogchoemqchpdnetxupbqntietiabpsmaoncnwvoutiugtagmmqsxtvxaoniiogtagmbpsmtuvvihpstpdvcrxhokvhxotawswquunewcgxptlcrxtevtubvewcnwwsxfsnptswtagakvoyyak";
//Kasiski test
for (int i = 0; i <= code.size() - 4; i++)
{
string temp = code.substr(i, 3);
int t=code.find(temp, i+1);//找下一个片断
if (t != string::npos)
{
cout << t-i << endl;//输出位置差
}
}
system("pause");
return 0;
}
获得输出:函数
114 114 114 114 39 39 24 87 18 18 18 72 27 18
求最大公约数,猜想密码片断长度为3ui
重合指数是字母串中两个随机元素相同的几率。
随机英文文本的IC老是大约为0.038。
而一段有意义的英文文本的IC老是大约为0.065。
能够用这个进一步确认密钥字的长度。
公式以下:
交互重合指数是在两个字符串中分别取两字符,它们相同的几率。
能够以此来判断两个字符串的相对位移。
相对位移为0时,交互重合指数接近0.065,其余状况下在0.0031到
0.0045之间。加密
如下为计数spa
const int step = 3;//密钥长度
int count[step][26] = { 0 };
for (int i = 0; i < code.size(); i++)
{
count[i%step][code[i] - 'a']++;
}
如下函数返回相对位移code
int shift(int *a,int *t )
{
int sum_a = 0, sum_t = 0;
for (int i = 0; i < 26; i++)
{
sum_a += a[i];
sum_t += t[i];
}
int index = -1;
int sum_max = 0;
for (int i = 0; i < 26; i++)
{
int sum = 0;
for (int j = 0; j < 26; j++)
{
sum += a[j] * t[(j + i) % 26];
}
if (sum > sum_a * sum_t * 0.05)//Ic>0.05,实际应该在0.065附近
{
cout << "index" << i << endl;
if (sum > sum_max) //若是有多个值大于0.05返回最大的
{
index = i;
sum_max = sum;
}
}
}
return index;
}
若是是简单的加法密码的话,能够直接和字母出现的统计几率表进行比较
前两种分析比较侧重于多表加密,在解决简单的密码的时候单表加密的统计分析会显得比较直接。
相似于index of coincidence的分析
像mod3=1的那一个部分,
5,20几率特别大,根据二者之间的差猜想为e和t;
mod3=0的部分语法猜想最开始是it的可能性比较大,那么移动为24,移动以后的几率相似。
mod3=2直接所有输出挑了一下……反正也才26种?
没有空格……原本英语就很差……QAQ 蛮好玩的233除了……常常感受本身在加密而不是解码……