资源预览内容
第1页 / 共17页
第2页 / 共17页
第3页 / 共17页
第4页 / 共17页
第5页 / 共17页
第6页 / 共17页
第7页 / 共17页
第8页 / 共17页
第9页 / 共17页
第10页 / 共17页
亲,该文档总共17页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
实实 验验 报报 告告实验课程名称实验课程名称 现代密码学现代密码学 学学 院院 理学院理学院 年级年级 08 专业班专业班 0303 学学 生生 姓姓 名名 丁朋丁朋 学学 号号 25 开开 课课 时时 间间 2010 至至 2011 学年第学年第 二二 学期学期总总 成成 绩绩教师签名教师签名实验项目 名 称实验一、古典密码(认识密码学)实验一、古典密码(认识密码学)成绩一、实验目的一、实验目的通过实现简单的古典密码算法,理解密码学的相关概念如明文(plaintext) 、密文 (ciphertext) 、加密密钥(encryption key)、解密密钥(decryption key) 、加密算法 (encryption algorithm)、解密算法(decryption algorithm)等。二、实验内容二、实验内容1)用 CC+语言实现仿射变换仿射变换(Affine)加/解密算法;2)用 CC+语言实现统计 26 个英文字母出现的频率的程序;3)利用仿射变换仿射变换加/解密程序对一段较长的英文文章 进行加密,再利用统计软件对明文和密文中字母出现的频率进行统计并作对比,观察有 什么规律。放射变换:加密加密: 26mod,bammEcba解密解密: 26mod1 ,bcacDmba其中 a, b 为密钥,且 gcd(a, 26)=125,0ba实验要求:加/解密程序对任意满足条件的 a、b 都能够处理。三、实验步骤三、实验步骤 (1)统计 26 个英文字母出现的频率的程序 #include #include #include using namespace std; void main()ifstream in(“a.txt“);vector s;vector n(26,0);for(int i=0;ix; )for(int i=0;i #include #include using namespace std; /判断两个数是不是互素(辗转相除)/ bool gcd(int a) int f=26,g,r; g=a; do r=f%g; f=g; g=r; while(r); if(f=1) return 1; else return 0; /求逆/ int inv(int a) int x,i; for(i=1;iz; if(z=0|z=1) / coutab; if(a25)|(b25) cout s;for(char x;inx; )s.push_back(int(x);for(int i=0;i s;for(char x;inx; )s.push_back(int(x);for(int i=0;i #include #include #include using namespace std; void main() /下面是密钥的产生/ int a31=1,1,0,0,1; for(int k=5;kkey; vector s,ss; if(key=1|key=2) if(key=1) coutc)int sum=0;for(int j=0;j32)i=(i+j-1)%31+1;elsei=i+8;s.push_back(int(c)sum);for(int kk=0;kkc)int sum=0;for(int j=0;j32) i=(i+j-1)%31+1;elsei=i+8;s.push_back(int(c)sum);for(int kk=0;kk using namespace std;/return next state unsigned char fn_feedback(unsigned char n,unsigned char c,unsigned char curr_state) unsigned char t = c unsigned char s = t /get first bit for(int i =1; i (n-1); /return next state void lfsr_output_byte(int n,unsigned char c,unsigned char init_state,unsigned char output_bytes,int byte_length) unsigned char next_state = init_state;for(int i = 0; i j ); next_state = fn_feedback(n, c, next_state); output_bytesi = temp; /output a char type of data in binary way void output_binary(unsigned char c) for(int i=0; icypher;for(int j=0;cypherj!=0;j+) cyphj=cypherjbj; cout #include #include using namespace std;int Euclid(int a,int n)/n 大于 a int x,y,r; x=n;y=a; for(int i=0;) if(y=0) return x; if(y=1) return y;r=x%y; x=y; y=r; double extenEuclid(double a,double n)/利用扩展的 EUCLID 计算 a mod n 的乘法逆元 double x1=1,x2=0,x3=n,y1=0,y2=1,y3=a,Q; double t1,t2,t3; for(int i=0;) if(y3=0) return x3;cout b; unsigned int N=n-1; for(int i=0,j=1; i+) if(jN) break; if( (N i) else b.push_back(0); j*=2; /将 n-1 表示成二进制形式 for(int k=0;k=0;ii-) x=d; d=(d*d)%n; if(d=1 if(bii=1) d=(d*a)%n; if(d!=1) return false; return true; double quickindex1(double a,double m,double n)/实现 am mod n 的运算 vector b; unsigned double N=m; for(int ii=0,j=1;ii+) if(jN) break;if(Nii) else b.push_back(0); j*=2; double c=0,d=1; for(int i=b.size()-1;i=0;i-) c*=2; d=(d*d)-int(d*d)/n)*n; if(bi=1) c+=1; d=(d*a)-int(d*a)/n)*n; return d; void transfer(char cypher,double c) int m100=0; for(int i=0,j=0;cypherj!=0;i+=2) if(cypherj= ) mi=0;mi+1=0; else mi=cypherj-64; if(mi10) mi+1=mi; mi=0; else mi+1=mi%10; mi=mi/10; j+; for(int k2=0;k22*strlen(cypher);k2+) coutmk2; coutendl; /int c100=0; for(int k=0,n=0;k2*strlen(cypher);k+=4) cn=mk*1000+mk+1*100+mk+2*10+mk+3; n+; for(;cn-11000;)/最后一个数填充零,不过此例可要可不要 cn-1*=10; void main() double c100=0; double a100=0; double b100=0; char cypher=“I LOVE THE PEOPLES REPUBLIC OF CHINA“; /对“我爱中华人民共和国”加解密transfer(cypher,c);/字母变数字的过程for(int k1=0;ck1!=0;k1+) coutck1“ “;/选取两个素数 p=563,q=823 double n=0, fn=0, p=563,q=823,d=0; n=p*q;fn=(q-1)*(p-1); /选 e 与 fn 互素 for(double e=2;efn;e+=3) if(Euclid(e,fn)=1) break; d=extenEuclid(e,fn);coutendl“密码和密钥 e d and n:“; coute“ “d“ “nendl; /加密过程 coutendl“加密:“; for(int i=0;ci!=0;i+) ai=quickindex1(ci,e,n); coutai“ “; coutendl“解密:“; /解密过程 for(int j=0;aj!=0;j+) bj=quickindex1(aj,d,n); coutbj“ “; coutendl; 四、实验结果及分析四、实验结果及分析
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号