-IT实验9-统计分析攻击实验

2020-03-27 08:48

云南大学数学与统计学院数学系信息与计算科学专业

云南大学数学与与统计学院

上机实践报告

一、实验目的

理解古典密码学、统计分析攻击、信息熵与密码体制的关系

二、实验内容

1. 古典密码体制Caesar密码的编程(参阅数据结构与算法主讲课教材的3.1.4节) 2. 任取一段较长的且有意义的英文片段,计算其明文熵。 3. 用Caesar密码加密上述英文片段,计算其密文熵。

4. 查阅英文字母的频度分布规律,对上述密文进行统计分析攻击。

三、实验环境

个人计算机,Java平台

对于非信息与计算科学专业的学生,可以选择任意编程平台

四、实验记录与实验结果分析

Caesar密码基本思想:

通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推X将变成A,Y变成B,Z变成C。由此可见,位数就是凯撒密码加密和解密的密钥。

所选英文片段:

Xi'an was once called “Chang'an” in the Han Dynasty. The connotation of this name is “permanent peace”. Xi'an marked the starting point of the world famous Silk Road. It obtained its present name in the year 1369. Xi'an ranks first on the list of the country's seven largest ancient capitals. From 11th century B.C. onwards, and for as long as more than 1100 years, Xi'an was successively established as the capital city by 15 kingdoms or feudal dynasties, including the Western Zhou, the Qin, the Han, the Sui and the Tang. It serves as an ancient capital city beyond comparison with regard to the number of dynasties and span of time. And it was regarded as one of the “Four Ancient Capitals of the World”, the other three being Rome, Athens, and Istanbul.

运行结果:

加密输入0,解密输入1:0

明文字符串:Xi'an was once called “Chang'an” in the Han Dynasty. The connotation of this

第 1 页 共 8 页

云南大学数学与统计学院数学系信息与计算科学专业

name is “permanent peace”. Xi'an marked the starting point of the world famous Silk Road. It obtained its present name in the year 1369. Xi'an ranks first on the list of the

country's seven largest ancient capitals. From 11th century B.C. onwards, and for as long as more than 1100 years, Xi'an was successively established as the capital city by 15 kingdoms or feudal dynasties, including the Western Zhou, the Qin, the Han, the Sui and the Tang. It serves as an ancient capital city beyond comparison with regard to the number of dynasties and span of time. And it was regarded as one of the “Four Ancient Capitals of the World”, the other three being Rome, Athens, and Istanbul.

移动位数:3

所得密文为:str =

Al'dq zdv rqfh fdoohg “Fkdqj'dq” lq wkh Kdq Gbqdvwb. Wkh frqqrwdwlrq ri wklv qdph lv “shupdqhqw shdfh”. Al'dq pdunhg wkh vwduwlqj srlqw ri wkh zruog idprxv Vlon Urdg. Lw rewdlqhg lwv suhvhqw qdph lq wkh bhdu 1369. Al'dq udqnv iluvw rq wkh olvw ri wkh frxqwub'v vhyhq odujhvw dqflhqw fdslwdov. Iurp 11wk fhqwxub E.F. rqzdugv, dqg iru dv orqj dv pruh wkdq 1100 bhduv, Al'dq zdv vxffhvvlyhob hvwdeolvkhg dv wkh fdslwdo flwb eb 15 nlqjgrpv ru ihxgdo gbqdvwlhv, lqfoxglqj wkh Zhvwhuq Ckrx, wkh Tlq, wkh Kdq, wkh Vxl dqg wkh Wdqj. Lw vhuyhv dv dq dqflhqw fdslwdo flwb ehbrqg frpsdulvrq zlwk uhjdug wr wkh qxpehu ri gbqdvwlhv dqg vsdq ri wlph. Dqg lw zdv uhjdughg dv rqh ri wkh “Irxu Dqflhqw Fdslwdov ri wkh Zruog”, wkh rwkhu wkuhh ehlqj Urph, Dwkhqv, dqg Lvwdqexo.

加密输入0,解密输入1:1

密文字符串:Al'dq zdv rqfh fdoohg “Fkdqj'dq” lq wkh Kdq Gbqdvwb. Wkh frqqrwdwlrq ri wklv qdph lv “shupdqhqw shdfh”. Al'dq pdunhg wkh vwduwlqj srlqw ri wkh zruog idprxv Vlon Urdg. Lw rewdlqhg lwv suhvhqw qdph lq wkh bhdu 1369. Al'dq udqnv iluvw rq wkh olvw ri wkh

frxqwub'v vhyhq odujhvw dqflhqw fdslwdov. Iurp 11wk fhqwxub E.F. rqzdugv, dqg iru dv orqj dv pruh wkdq 1100 bhduv, Al'dq zdv vxffhvvlyhob hvwdeolvkhg dv wkh fdslwdo flwb eb 15 nlqjgrpv ru ihxgdo gbqdvwlhv, lqfoxglqj wkh Zhvwhuq Ckrx, wkh Tlq, wkh Kdq, wkh Vxl dqg wkh Wdqj. Lw vhuyhv dv dq dqflhqw fdslwdo flwb ehbrqg frpsdulvrq zlwk uhjdug wr wkh qxpehu ri gbqdvwlhv dqg vsdq ri wlph. Dqg lw zdv uhjdughg dv rqh ri wkh “Irxu Dqflhqw Fdslwdov ri wkh Zruog”, wkh rwkhu wkuhh ehlqj Urph, Dwkhqv, dqg Lvwdqexo.

输入密钥:3

所得明文为:str =

Xi'an was once called “Chang'an” in the Han Dynasty. The connotation of this name is “permanent peace”. Xi'an marked the starting point of the world famous Silk Road. It obtained its present name in the year 1369. Xi'an ranks first on the list of the country's seven largest ancient capitals. From 11th century B.C. onwards, and for as long as more than 1100 years, Xi'an was successively established as the capital city by 15 kingdoms or feudal dynasties, including the Western Zhou, the Qin, the Han, the Sui and the Tang. It serves as an ancient capital city beyond comparison with regard to the number of dynasties and span of time. And it was regarded as one of the “Four Ancient Capitals of the World”, the other three being Rome, Athens, and Istanbul.

第 2 页 共 8 页

云南大学数学与统计学院数学系信息与计算科学专业

输入需要进行熵计算明文(密文):Xi'an was once called “Chang'an” in the Han Dynasty. The connotation of this name is “permanent peace”. Xi'an marked the starting point of the world famous Silk Road. It obtained its present name in the year 1369. Xi'an ranks first on the list of the country's seven largest ancient capitals. From 11th century B.C. onwards, and for as long as more than 1100 years, Xi'an was successively established as the capital city by 15 kingdoms or feudal dynasties, including the Western Zhou, the Qin, the Han, the Sui and the Tang. It serves as an ancient capital city beyond comparison with regard to the number of dynasties and span of time. And it was regarded as one of the “Four Ancient Capitals of the World”, the other three being Rome, Athens, and Istanbul.

出现频率最高的字母为: e

明文(密文)熵为: H =0.1098

输入需要进行熵计算明文(密文):Al'dq zdv rqfh fdoohg “Fkdqj'dq” lq wkh Kdq Gbqdvwb. Wkh frqqrwdwlrq ri wklv qdph lv “shupdqhqw shdfh”. Al'dq pdunhg wkh vwduwlqj srlqw ri wkh zruog idprxv Vlon Urdg. Lw rewdlqhg lwv suhvhqw qdph lq wkh bhdu 1369. Al'dq udqnv iluvw rq wkh olvw ri wkh frxqwub'v vhyhq odujhvw dqflhqw fdslwdov. Iurp 11wk fhqwxub E.F. rqzdugv, dqg iru dv orqj dv pruh wkdq 1100 bhduv, Al'dq zdv vxffhvvlyhob hvwdeolvkhg dv wkh fdslwdo flwb eb 15 nlqjgrpv ru ihxgdo gbqdvwlhv, lqfoxglqj wkh Zhvwhuq Ckrx, wkh Tlq, wkh Kdq, wkh Vxl dqg wkh Wdqj. Lw vhuyhv dv dq dqflhqw fdslwdo flwb ehbrqg frpsdulvrq zlwk uhjdug wr wkh qxpehu ri gbqdvwlhv dqg vsdq ri wlph. Dqg lw zdv uhjdughg dv rqh ri wkh “Irxu Dqflhqw Fdslwdov ri wkh Zruog”, wkh rwkhu wkuhh ehlqj Urph, Dwkhqv, dqg Lvwdqexo. 出现频率最高的字母为: h

明文(密文)熵为: H =0.0493

程序代码:

Caesar密码的加密与解密

flag=input('加密输入0,解密输入1:'); if flag==0

str=input('明文字符串:','s') k=input('移动位数:') n=length(str); for i=1:n

if str(i)>=97&str(i)<=(122-k) str(i)=str(i)+k; else

if str(i)>(122-k)&str(i)<=122 str(i)=str(i)-26+k; end end

if str(i)>=65&str(i)<=(90-k) str(i)=str(i)+k; else

if str(i)>(90-k)&str(i)<=90 str(i)=str(i)-26+k;

第 3 页 共 8 页

云南大学数学与统计学院数学系信息与计算科学专业

end end end

disp('所得密文为:') str else

str=input('密文字符串:','s') k=input('输入密钥:') n=length(str); for i=1:n

if str(i)>=(97+k)&str(i)<=122 str(i)=str(i)-k; else

if str(i)>=97&str(i)<(97+k) str(i)=str(i)+26-k; end end

if str(i)>=(65+k)&str(i)<=90 str(i)=str(i)-k; else

if str(i)>=65&str(i)<(65+k) str(i)=str(i)+26-k; end end end

disp('所得明文为:') str end

计算明文与密文熵,及频度分布规律:

str=input('输入需要进行熵计算明文(密文):','s'); y(1:52)=0; z=0;

asc=[65:90,97:122]; n=length(str); for i=1:n

for j=1:52

if str(i)==asc(j) y(j)=y(j)+1; z=z+1; end end end

for i=1:52

p(i)=y(i)/z; end

[a,b]=max(p);

disp('出现频率最高的字母为:') if b>=1&b<=26 char(64+b) else

第 4 页 共 8 页

云南大学数学与统计学院数学系信息与计算科学专业

char(70+b) end H=0;

for i=1:52 if p(i)~=0

H=H-p(i)*log2(p(i)); else

break; end end

disp('明文(密文)熵为:') H

五、实验体会

①通过此次试验,对凯撒密码有了更深入的了解,caesar密码是已知的最早的单表代换密码,采用加法加密的方法,将字母表的每个字母,用它后面的第key个字母代替,此实验中key取3. 明文字母w就变成了密文的z,依次类推,o将变成r,等由此可见,位数就是凯撒密码加密和解密的密钥。

②因为英文和其它语言中每个字母都有一定的使用频率,破解者根据这些频率就可以破译,比如在实验中e出现的频率是最高的,可以根据密码本来判断密钥。

六、参考文献

[1]Thomas M. Cover, Joy A. Thomas. Elements of Information Theory (2 Edition) [M]. John Wiley & Sons, Inc. Chapter 7

[2]王永利、阮沈勇等,MATLAB程序设计,电子工业出版社,2004-1

nd

第 5 页 共 8 页


-IT实验9-统计分析攻击实验.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:18《颐和园》教学设计

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: