频率分析的单表替换密码破译报告

原理分析

单表替换,即采用一个乱序字母表来替代明文字母,明文字母保持一一对应关系。 26 26 26个英文字母,即有 26 ! 26! 26种映射关系表,代表密钥空间有 26 ! 26! 26。穷举方法进行暴力破解是不可行的。
单表替换加密可实现破解,主要基于以下2点:

  1. 单表替换加密方法,有一个明显的缺陷,即,没有改变字母使用的统计特性;

根据对大量英文文献的统计,发现,各字母出现频率非常稳定。下表是英文字母的频率分布图

字母 频率 字母 频率 字母 频率
A 8.167 B 1.492 C 2.782
D 4.253 E 12.702 F 2.228
G 2.015 H 6.094 I 6.966
J 0.153 K 0.772 L 4.026
M 2.406 N 6.749 O 7.507
P 1.929 Q 0.095 R 5.987
S 6.327 T 9.056 U 2.758
V 0.978 W 2.36 X 0.15
Y 1.974 Z 0.074

其中,e的使用频率远高于其他字母的使用频率,按照频率高低排序,其顺序为
frequencyLetter = [‘e’, ‘t’, ‘a’, ‘o’, ‘i’, ‘n’, ‘s’, ‘h’, ‘r’, ‘d’, ‘l’, ‘c’, ‘u’, ‘m’, ‘w’, ‘f’, ‘g’, ‘y’, ‘p’, ‘b’, ‘v’, ‘k’, ‘j’, ‘x’, ‘q’, ‘z’]。

  1. 英文常用的一元词和二元词甚至是三元词个数极其有限的,可以实现逐一匹配,进行猜测。

常用的一元词,按频率高到低
formalSingle = [“a”, “i”, “s”, “t”, “m”, “d”];
常用的二元词,按频率高到低
formalDouble = [“of”, “to”, “in”, “it”, “is”, “be”, “as”, “at”, “so”, “we”, “he”, “by”, “or”, “on”, “do”, “if”, “me”, “my”, “up”, “an”, “go”, “no”, “us”, “am”, “ll”];
常用的三元词, 按频率高低列出
formalTriple = [“the”, “and”, “for”, “are”, “but”, “not”, “you”, “all”, “any”, “can”, “had”, “her”, “was”, “one”, “our”, “out”, “day”, “get”, “has”, “him”, “his”, “how”, “man”, “new”, “now”, “old”, “see”, “two”, “way”, “who”, “boy”, “did”, “its”, “let”, “put”, “say”, “she”, “too”, “use”, “etc”]。

算法描述

Step 1. 定义常见的一元词formalSingle、二元词formalDouble以及3元词formalTriple。字母使用频率顺序表frequencyLetter。
Step 2. 对密文进行预处理。① 统计密文中各字母出现的频数singleFrequency,高低排序singleSet;② 统计各单词出现的频率combinedFrequency;③ 找出一元词、二元词和三元词出现频率高的,且超过频率阈值的,存储至simpleCipherArray。
Step 3. 密文中一元词按频率依次对应formalSingle中一元词,保存其映射关系(密文字母<->明文字母)
Step 4. 将二元单词中频数高的与formalDouble中字母映射。
Step 5. 检查相互冲突的映射关系
① 如果出现冲突,则一元单词得出的映射关系不变,寻找下一个匹配的二元单词匹配的映射。直至匹配到formalDouble最后一个二元词,仍出现冲突,则跳至步骤3,改变一元词的匹配关系。
② 如果不冲突,则保留二元词的映射关系。
Step 6. 将密文中的一元词和二元词与常见的一元词formalSingle、二元词formalDouble匹配完,且不发生冲突的保留。密文中剩余的字母,按频率关系依次对应字母使用频率顺序表frequencyLetter中未使用的字母,完成映射表。
Step 7. 根据映射表,恢复出明文信息recover,查看recover,根据三元词以及上下文继续修正映射关系。

破解实例

用python实现算法所描述的过程,输入需破译的密文信息passage。
passage =
hzsrnqc klyy wqc flo mflwf ol zqdn nsoznj wskn lj xzsrbjnf, wzsxz gqv zqhhnf ol ozn glco zlfnco hnlhrn; nsoznj jnrqosdnc lj fnqj kjsnfbc, wzsxz sc xnjoqsfrv gljn efeceqr. zn rsdnb qrlfn sf zsc zlecn sf cqdsrrn jlw, wzsoznj flfn hnfnojqonb. q csfyrn blgncosx cekksxnb ol cnjdn zsg. zn pjnqmkqconb qfb bsfnb qo ozn xrep, qo zlejc gqozngqosxqrrv ksanb, sf ozn cqgn jllg, qo ozn cqgn oqprn, fndnj oqmsfy zsc gnqrc wsoz loznj gngpnjc, gexz rncc pjsfysfy q yenco wsoz zsg; qfb wnfo zlgn qo naqxorv gsbfsyzo, lfrv ol jnosjn qo lfxn ol pnb. zn fndnj ecnb ozn xlcv xzqgpnjc wzsxz ozn jnkljg hjldsbnc klj soc kqdlejnb gngpnjc. zn hqccnb onf zlejc leo lk ozn ownfov-klej sf cqdsrrn jlw, nsoznj sf crnnhsfy lj gqmsfy zsc olsrno

Step 1. python 代码实现
经一元词和二元词的匹配无冲突后,得到如下映射表
[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]-密文
[-,-,-,-,-,n,-,-,-,f,-,o,-,e,t,-,a,-,i,-,-,-,r,-,-,h]-明文
再按照字母使用频率对照关系之后,得到<密文-明文>映射表 encodeTable
[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]-密文
[j,c,s,y,m,n,l,p,-,f,w,o,k,e,t,b,a,d,i,-,-,v,r,u,g,h]-明文
根据映射表encodeTable,得到恢复结果: recover =
phideas wogg ras not knorn to haye eithef riwe of uhidcfen rhiuh lav happen to the lost honest peopde eithef fedatiyes of neaf wfiencs rhiuh is ueftaindv lofe mnmsmad he diyec adone in his homse in sayidde for rhithef none penetfatec a singde colestiu smwwiuec to sefye hil he bfeakwastec anc cinec at the udmb at homfs lathelatiuaddv wijec in the sale fool at the sale tabde neyef taking his leads rith othef lelbefs lmuh dess bfinging a gmest rith hil anc rent hole at ejautdv licnight ondv to fetife at onue to bec he neyef msec the uosv uhalbefs rhiuh the fewofl pfoyices wof its wayomfec lelbefs he passec ten homfs omt ow the trentvwomf in sayidde for eithef in sdeeping of laking his toidet
Step 2. 手动修正
再根据常用单词匹配,手动修正recover.
ras 匹配 was, knorn匹配 known,=> r->w;
peopde 匹配 people, adone 匹配alone, tabde 匹配 table, sdeeping 匹配sleeping, => d->l;
eithef 匹配 either, neaf 匹配 near, othef 匹配other, => f->r;
homse 匹配 house, gmest 匹配guest, omt 匹配out , => m->u;
hil 匹配 his/him l->s 与 ras 匹配 was 冲突,不做修改 => l->m;
anc 匹配 and, passec 匹配passed, => c->d;
onue 匹配 once,=> u->c;
rith 匹配 rich, t->c 与 u-> c冲突,保留
ow 匹配 of/on w->n, 一元词与二元词匹配结果,先不修改, =>w->f;
修改之后得到恢复信息:recover1 =
phileas fogg was not known to haye either wife or children which mav happen to the most honest people either relatiyes or near friends which is certainlv more unusual he liyed alone in his house in sayille row whither none penetrated a single domestic sufficed to serye him he breakfasted and dined at the club at hours mathematicallv fijed in the same room at the same table neyer taking his meals with other members much less bringing a guest with him and went home at ejactlv midnight onlv to retire at once to bed he neyer used the cosv chambers which the reform proyides for its fayoured members he passed ten hours out of the twentvfour in sayille row either in sleeping or making his toilet.
继续根据上下文修正recover1.
haye 匹配 have, neyer 匹配 never ,relatiyes 匹配relatives, liyed 匹配lived, serye 匹配saves,=> y->v (->表示替换)
Mav 匹配 may, onlv 匹配 only, certainlv 匹配certainly => v->y
得到恢复:recover2 =
phileas fogg was not known to have either wife or children which may happen to the most honest people either relatives or near friends which is certainly more unusual he lived alone in his house in saville row whither none penetrated a single domestic sufficed to serve him he breakfasted and dined at the club at hours mathematically fijed in the same room at the same table never taking his meals with other members much less bringing a guest with him and went home at ejactly midnight only to retire at once to bed he never used the cosy chambers which the reform provides for its favoured members he passed ten hours out of the twentyfour in saville row either in sleeping or making his toilet
继续修正recover2
fijed 匹配 fixed, ejactly 匹配 exactly,=> j->x
得到恢复信息: recover3 =
phileas fogg was not known to have either wife or children which may happen to the most honest people either relatives or near friends which is certainly more unusual he lived alone in his house in saville row whither none penetrated a single domestic sufficed to serve him he breakfasted and dined at the club at hours mathematically fixed in the same room at the same table never taking his meals with other members much less bringing a guest with him and went home at exactly midnight only to retire at once to bed he never used the cosy chambers which the reform provides for its favoured members he passed ten hours out of the twentyfour in saville row either in sleeping or making his toilet。
经过三轮手动修正,已全部恢复明文。
python代码运行结果:

代码参考:danbiaotihuan_python

你可能感兴趣的:(python,密码学)