您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 信息安全古典密码之凯撒密码
《网络攻击与防御》实验报告课程名称:信息安全技术实验名称:古典密码之凯撒密码指导教师:学生姓名:组号:实验日期:实验地点:实验成绩:计算机科学与技术学院计算机系网络教研室制一、实验目的(1)编程实现凯撒加密、解密算法,理解密码学基础知识,初步建立密码学思维方式。(2)通过不断增加凯撒解密难度,理解唯密文解密,提高解密性能。二、实验环境操作系统:Windows10操作系统软件工具:C++语言编译环境三、实验内容与实验要求(1)在允许输入密码条件下,编程实现凯撒密码加解密解密。要求:①从一文本文件读入英文文章(明文或密文)。②对读入内容加密或解密后写入另一文本文件。(2)在不允许输入密码条件下,编程实现解密凯撒密码加密密文。要求绘制三种情况下解密程序流程图,说明不同解密程序存在的不足。程序需要计算、显示解密使用时间(单位:ms)。①已知c1=wklvlvderrn,求p1。(初级解密)问:两次使用凯撒,能否正确解密?(字符串用凯撒加密后的结果再用凯撒加密一次。)②已知c1=gokbocdenoxdc,或c1=zhduhvwxghqwv,求p1。(中级解密)③已知c1=rxwvlghwkheleoh,wkhvhvlazrugvduhwkhprvwidprxvlqdoowkholwhudwxuhriwkhzruog.wkhbzhuhvsrnhqebkdpohwzkhqkhzdvwklqnlqjdorxg,dqgwkhbduhwkhprvwidprxvzrugvlqvkdnhvshduhehfdxvhkdpohwzdvvshdnlqjqrwrqobiruklpvhoiexwdovriruhyhubwklqnlqjpdqdqgzrpdq.wrehruqrwwreh,wrolyhruqrwwrolyh,wrolyhulfkobdqgdexqgdqwobdqghdjhuob,ruwrolyhgxoobdqgphdqobdqgvfdufhob.dsklorvrskhurqfhzdqwhgwrnqrzzkhwkhukhzdvdolyhruqrw,zklfklvdjrrgtxhvwlrqiruhyhubrqhwrsxwwrklpvhoirffdvlrqdoob.khdqvzhuhglwebvdblqj:lwklqn,wkhuhiruhdp.,求p1。(高级解密)对给定较长密文文件进行解密测试,测试结果填入表。要求密文的内容不少于1000个英文单词,使用凯撒密码加密,加密密码保密。正确率=正确单词数/单词总数,智能程度:优秀(解密结果正确与否不需要人工判断)、一般。表四、实验过程与分析(1)在允许输入密码条件下的实验结果。从file1中读入明文,将密文存放在file2中(2)在不允许输入密码条件下的实验结果①在有单个字母的情况下,初级解密②无单个字母下,中级解密,依次用1-25作为密钥。③高级解密初级解密:读入是否成功?是查到单个字母ch否读取失败Primary(ch)结束开始读file3.txt将该字母与a换算得到密钥key将其他字母换算得到对应的密文输出该密文至显示器将密文显示中级解密:开始读file3.txt文件是否读取成功是否输出读取失败str[i]-=keyMediate()N++N25是结束否是Intermediate()密文显示N=1无单字母高级解密:开始读file4.txt是否成功是计算文件中每个字母出现的频率q否输出读取失败提示Intj=0读入每个字母的大数据概率pi++计算sum+=p[i]*q[t];i25是找出sum值最接近0.065379的一次返回密钥Inti=0j++否j25是否进行解密并打印结束五、实验结果总结是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。使用时,加密者查找明文字母表中需要加密的消息中的每一个字母所在位置,并且写下密文字母表中对应的字母。需要解密的人则根据事先已知的密钥反过来操作,得到原来的明文。源代码:#includefstream#includeiostream#includestring#includecmath#includeWindows.h#defineN1000#defineTARGET0.065379#defineLIST_LEN26usingnamespacestd;constdoublep[LIST_LEN]={0.082,0.015,0.028,0.042,0.127,0.022,0.02,0.061,0.07,0.001,0.008,0.04,0.024,0.067,0.075,0.019,0.001,0.06,0.063,0.09,0.028,0.01,0.024,0.02,0.001,0.001};stringdict[7]={we,be,to,am,you,are,the};voidfile_print(char*filename){ifstreaminfile;charch;inti=0;infile.open(filename,ios::in);if(!infile){coutopenerror!endl;exit(1);}while(infile.get(ch)){coutch;}coutendl;infile.close();}intnumber(char*filename){ifstreaminfile;charch;inti=0;infile.open(filename,ios::in);if(!infile){coutopenerror!endl;exit(1);}while(infile.get(ch)){i++;}infile.close();returni;}voidEncrypt(){ifstreaminfile;ofstreamoutfile;charch,*code;unsignedcharstr[N];code=newchar(2);cout明文为:endl;file_print(file1.txt);intn=number(file1.txt);infile.open(file1.txt,ios::in);if(!infile){coutopenerror!endl;exit(1);}cout请输入一位英文字符密码:endl;cincode;code[1]='\0';//coutcode[1];if(code[1]!='\0')cout输入错误,请输入一位英文字符:endl;if(code[0]=65&&code[0]=90){code[0]+=32;}for(inti=0;in;i++){infilestr[i];}outfile.open(file2.txt);if(!outfile){coutopenerror!endl;exit(1);}intkey=code[0]-97;cout密钥为:keyendl;for(i=0;str[i]!=0;i++){if(str[i]=65&&str[i]=90)str[i]+=32;if(str[i]97||str[i]122)continue;str[i]+=key;if(str[i]122){str[i]-=26;}outfile.put(str[i]);}infile.close();outfile.close();cout密文为:endl;file_print(file2.txt);}voidprimary(charc){DWORDstart_time=GetTickCount();ifstreaminfile;charch;unsignedcharstr[N];infile.open(file3.txt,ios::in);if(!infile){coutopenerror!endl;exit(1);}for(inti=0;infile.get(ch);i++){str[i]=ch;//coutstr[i];}intkey=abs(c-'a');//coutkey;cout密文为:endl;file_print(file3.txt);intn=number(file3.txt);//coutn;cout明文为:endl;for(i=0;in;i++){if(str[i]==''){str[i]=32;coutstr[i];}if(str[i]97||str[i]122)coutstr[i];else{str[i]-=key;if(str[i]97)str[i]+=26;coutstr[i];}}coutendl;infile.close();DWORDend_time=GetTickCount();cout解密时间为:end_time-start_timems!endl;}voidMediate_senior(unsignedcharstr[],intn){intkey=1;intsimilar=0;for(inti=0;in;i++){if(str[i]==''){str[i]=32;coutstr[i];}if(str[i]97||str[i]122)coutstr[i];else{str[i]-=key;if(str[i]97)str[i]+=26;coutstr[i];}}}voidintermediate(){DWORDstart_time=GetTickCount();ifstreaminfile;charch;unsignedcharstr[N];infile.open(file3.txt,ios::in);if(!infile){coutopenerror!endl;exit(1);}cout密文为:endl;for(inti=0;infile.get(ch);i++){str[i]=ch;coutch;}coutendl;for(intn=1;n=25;n++){coutk=n'';Mediate_senior(str,number(file3.txt));printf(\n);}DWORDend_time=GetTickCount();cout解密时间为:end_time-start_timems!endl;}/*统计filename中字母出现频率,存放在数组q中*/voidcount(constcharfilename[],doubleq[LIST_LEN]){inti,len=0;/*len为字母总个数约等于密文长度*/FILE*fin=fopen(filename,r);charch;for(i=0;iLIST_LEN;i++)q[i]=0;while((ch=fgetc(fin))!=EOF){if(isalpha(ch)){len++;ch=tolower(ch);q[ch-'a']+=1;}}fclose(fin);for(i=0;iLIST_LEN;i++)q[i]/=len;}/*破解密钥key,并且将其中数组q存放密文字母出现频率*/intanalysis(constcharfilename[],doubleq[LIST_LEN]){inti,j,key=0;doubleeps=1;/*eps存储与TARGET最小差值*/count(filename,q);for(j=0;jLIST_LEN;j++){/*变量j穷举密钥*/doublesum=0,tem;for(i=0;iLIST_LEN;i++){/*求sum{p[i]*q[i+j]}之和*/intt=(i+j)%26;sum+=
本文标题:信息安全古典密码之凯撒密码
链接地址:https://www.777doc.com/doc-8541534 .html