现代密码学实验题目代码.docx

上传人:b****6 文档编号:7119216 上传时间:2023-01-19 格式:DOCX 页数:44 大小:41.12KB
下载 相关 举报
现代密码学实验题目代码.docx_第1页
第1页 / 共44页
现代密码学实验题目代码.docx_第2页
第2页 / 共44页
现代密码学实验题目代码.docx_第3页
第3页 / 共44页
现代密码学实验题目代码.docx_第4页
第4页 / 共44页
现代密码学实验题目代码.docx_第5页
第5页 / 共44页
点击查看更多>>
下载资源
资源描述

现代密码学实验题目代码.docx

《现代密码学实验题目代码.docx》由会员分享,可在线阅读,更多相关《现代密码学实验题目代码.docx(44页珍藏版)》请在冰豆网上搜索。

现代密码学实验题目代码.docx

现代密码学实验题目代码现代密码学实验题目代码*大学学生实验报告实验课程名称现代密码学学院*年级*专业班*学生姓名*学号*开课时间200-至200-学年第二学期总成绩教师签名实验项目名称实验一、古典密码(认识密码学)成绩一、实验目的通过实现简单的古典密码算法,理解密码学的相关概念如明文(plaintext)、密文(ciphertext)、加密密钥(encryptionkey)、解密密钥(decryptionkey)、加密算法(encryptionalgorithm)、解密算法(decryptionalgorithm)等。

二、实验内容1)用CC+语言实现仿射变换(Affine)加/解密算法;2)用CC+语言实现统计26个英文字母出现的频率的程序;3)利用仿射变换加/解密程序对一段较长的英文文章进行加密,再利用统计软件对明文和密文中字母出现的频率进行统计并作对比,观察有什么规律。

放射变换:

加密:

解密:

其中a,b为密钥,且gcd(a,26)=1实验要求:

加/解密程序对任意满足条件的a、b都能够处理。

三、实验步骤

(1)统计26个英文字母出现的频率的程序#include#include#includeusingnamespacestd;voidmain()ifstreamin(a.txt);vectors;vectorn(26,0);for(inti=0;ix;)for(inti=0;i26;+i)if(int(x)=si)ni+;floatsum=0.0;for(intj=0;j26;+j)sum+=nj;cout统计结果如下:

endl;for(intk=0;k26;+k)/nk=nk/sum;coutchar(k+97)出现的概率为:

nk/sumendl;/coutnkendl;

(2)仿射变换加/解密程序对一段较长的英文文章进行加密#include#include#includeusingnamespacestd;/判断两个数是不是互素(辗转相除)/boolgcd(inta)intf=26,g,r;g=a;dor=f%g;f=g;g=r;while(r);if(f=1)return1;elsereturn0;/求逆/intinv(inta)intx,i;for(i=1;i=30;+i)if(26*i+1)%a=0)x=(26*i+1)/a;break;returnx;/voidmain()cout请你选择操作密码的方式:

endl0表示加密endl1表示解密z;if(z=0|z=1)/cout请输入密钥a和b:

ab;if(a25)|(b25)couta,b的输入范围有错!

endl;elseif(gcd(a)=0)cout密钥a有误,与26不互素endl;elseif(z=0)/加密算法ifstreamin(a.txt);ofstreamout(b.txt);vectors;for(charx;inx;)s.push_back(int(x);for(inti=0;is.size();+i)si=(a*(si-97)+b)%26;outchar(si+97);outendl;cout加密成功!

明文请见“b.txt”endl;else/解密算法ifstreamin(b.txt);ofstreamout(a.txt);vectors;for(charx;inx;)s.push_back(int(x);for(inti=0;is.size();+i)si=inv(a)*(si-97-b+26)%26;outchar(si+97);outendl;cout解密成功!

密文请见“a.txt”endl;/elsecout所选操作无效!

endl;四、实验结果及分析该程序是对文件进行操作,结果如下:

(1)统计26个英文字母出现的频率的程序

(2)仿射变换加/解密程序对一段较长的英文文章进行加密下面是文本内容:

实验项目名称实验二、流密码(认识LFSR及流密码)成绩一、实验目的通过实现简单的线性反馈移位寄存器(LFSR),理解LFSR的工作原理、本原多项式重要意义。

二、实验内容1)利用CC+语言实现LFSR(其中LFSR已给定);2)通过不同初始状态生成相应的序列,并观察他们的周期有什么特点;3)利用生成的序列对文本进行加/解密(按对应位作模2加运算)。

其中的LFSR为:

三、实验步骤#include#include#include#includeusingnamespacestd;voidmain()/下面是密钥的产生/inta31=1,1,0,0,1;for(intk=5;k31;+k)ak=(ak-2+ak-5)%2;cout密钥如下:

endl;for(intjj=0;jj31;+jj)coutajj;coutendl;/inti=0,key;cout请选择操作方式:

1-加密2-解密key;vectors,ss;if(key=1|key=2)if(key=1)cout加密成功,密文见out.txtc)intsum=0;for(intj=0;j32)i=(i+j-1)%31+1;elsei=i+8;s.push_back(int(c)sum);for(intkk=0;kks.size();+kk)outchar(skk);if(key=2)cout解密成功,明文见in.txtc)intsum=0;for(intj=0;j32)i=(i+j-1)%31+1;elsei=i+8;s.push_back(int(c)sum);for(intkk=0;kks.size();+kk)outchar(skk);/elsecout操作无效!

endl;四、实验结果及分析在“in.txt”中输入如下内容:

实验结果如下:

得到密文“out.txt”如下:

实验项目名称实验三、流密码(生成非线性序列)成绩一、实验目的以LFSR序列为基础,生成非线性序列,并利用该序列对文件进行加密、解密。

二、实验内容1)利用CC+实现Geffe序列生成器及J-K触发器;2)利用生成的非线性序列对文件进行加密、解密(按对应位作模2加运算)。

三、实验步骤#includeusingnamespacestd;/returnnextstateunsignedcharfn_feedback(unsignedcharn,unsignedcharc,unsignedcharcurr_state)unsignedchart=c&curr_state;unsignedchars=t&(unsignedchar)128;/getfirstbitfor(inti=1;in;i+)s=s(ti)&(unsignedchar)128);return(unsignedchar)(curr_state(n-1);/returnnextstatevoidlfsr_output_byte(intn,unsignedcharc,unsignedcharinit_state,unsignedcharoutput_bytes,intbyte_length)unsignedcharnext_state=init_state;for(inti=0;ibyte_length;i+)unsignedchartemp=(unsignedchar)0;for(intj=0;jj);next_state=fn_feedback(n,c,next_state);output_bytesi=temp;/outputachartypeofdatainbinarywayvoidoutput_binary(unsignedcharc)for(inti=0;i8;i+)if(ci)&(unsignedchar)128)cout1;elsecout0;voidGeffe(unsignedcharbuf,unsignedcharbuf1,unsignedcharbuf2,unsignedcharb)for(inti=0;i10;i+)bi=bufi*buf1i+buf2i*buf1i+buf2i;output_binary(unsignedchar)bi);voidJK(unsignedcharbuf,unsignedcharbuf1,unsignedcharc)for(inti=1;i11;i+)ci=(bufi+buf1i+1)*ci-1+bufi;output_binary(unsignedchar)ci);voidcypt(unsignedcharb)unsignedcharcypher10,cyph10;coutcypher;for(intj=0;cypherj!

=0;j+)cyphj=cypherjbj;coutcyphj;coutendl;for(intk=0;kj;k+)cypherk=cyphkbk;coutcypherk;coutendl;intmain(intargc,char*argv)unsignedcharbuf10,buf110,buf210,b100,c100;/函数f初始状态152lfsr_output_byte(5,(unsignedchar)144,(unsignedchar)152,buf,10);lfsr_output_byte(5,(unsignedchar)44,(unsignedchar)152,buf1,10);lfsr_output_byte(5,(unsignedchar)24,(unsignedchar)152,buf2,10);Geffe(buf,buf1,buf2,b);c0=0;JK(buf,buf1,c);coutendl;coutGeffeoperate:

endl;cypt(b);coutendl;coutJ-Koperate:

endl;cypt(c);return0;四、实验结果及分析实验项目名称实验四、DES算法的实现成绩一、实验目的通过实现DES算法,加深对DES算法的理解,同时学习组合密码常用的代换、移位等运算的实现。

二、实验内容1)利用CC+实现DES算法的加、解密运算。

三、实验步骤定义头文件:

yxyDES.h#ifndefyxyDESH#defineyxyDESH#include#include#include#includeusingnamespacestd;classyxyDESpublic:

yxyDES();yxyDES();voidInitializeKey(string);voidEncryptData(string);voidDecryptData(string);voidEncryptAnyLength(string);voidDecryptAnyLength(string);voidSetCiphertext(char*value);char*GetCiphertext();voidSetPlaintext(char*value);char*GetPlaintext();char*GetCiphertextAnyLength();char*GetPlaintextAnyLength();private:

charSubKeys1648;charszCiphertext16;charszPlaintext8;charszFCiphertextAnyLength8192;charszFPlaintextAnyLength4096;voidCreateSubKey(char*);voidFunctionF(char*,char*,int);voidInitialPermuteData(string,char*,bool);voidExpansionR(char*,char*);voidXOR(char*,char*,int,char*);stringCompressFuncS(char*);voidPermutationP(string,char*);stringFillToEightBits(string);voidCleanPlaintextMark();stringHexCharToBinary(char);stringHexIntToBinary(int);stringBinaryToString(char*,int,bool);intSingleCharToBinary(char);charSingleBinaryToChar(int);#endif构造主文件:

#pragmahdrstop#includeyxyDES.h/-#pragmapackage(smart_init)/permutedchoicetable(PC1)conststaticintPC1_Table56=57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36,63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4;/permutedchoicekey(PC2)conststaticintPC2_Table48=14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32;/numberleftrotationsofpc1conststaticintShift_Table16=1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1;/initialpermutation(IP)conststaticintIP_Table64=58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7;/expansionoperationmatrix(E)staticconstintE_Table48=32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1;/The(in)famousS-boxesconststaticintS_Box8416=/S114,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13,/S215,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9,/S310,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12,/S47,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14,/S52,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3,/S612,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13,/S74,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12,/S813,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11;/32-bitpermutationfunctionPusedontheoutputoftheS-boxesconststaticcharP_Table32=16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25;/finalpermutationIP-1conststaticcharIPR_Table64=40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25;yxyDES:

yxyDES()yxyDES:

yxyDES()voidyxyDES:

InitializeKey(strings)/convert8char-byteskeyto64binary-bitsinti;charsz_64key64=0;intiTmpBit64=0;for(i=0;i3(i&7)&1;/a=0x61=0110,0001/afterthis,ais1000,0110/letmeconvertittorightfor(intj=0;j8;j+)for(intk=0;k8;k+)sz_64key8*j+k=SingleBinaryToChar(iTmpBit8*(j+1)-(k+1);/PC1charsz_56key56=0;for(i=0;i56;i+)sz_56keyi=sz_64keyPC1_Tablei-1;CreateSubKey(sz_56key);stringyxyDES:

HexCharToBinary(charch)switch(ch)case0:

return0000;case1:

return0001;case2:

return0010;case3:

return0011;case4:

return0100;case5:

return0101;case6:

return0110;case7:

return0111;case8:

return1000;case9:

return1001;casea:

return1010;caseb:

return1011;casec:

return1100;cased:

return1101;casee:

return1110;casef:

return1111;default:

return;stringyxyDES:

HexIntToBinary(inti)switch(i)case0:

return0000;case1:

return0001;case2:

return0010;case3:

return0011;case4:

return0100;case5:

return0101;case6:

return0110;case7:

return0111;case8:

return1000;case9:

return1001;case10:

return1010;case11:

return1011;case12:

return1100;case13:

return1101;case14:

return1110;case15:

return1111;default:

return;stringyxyDES:

BinaryToString(char*szSource,intlen,boolbType)/bType=trueisBinarytoHex/elseisBinarytoCharintilen;if(len%8!

=0)return;elseilen=len/8;strings_return=;for(inti=0;iilen;i+)charszTmp88=0;intiCh=0;memcpy(szTmp8,szSource+8*i,8);for(intj=0;j8;j+)iCh+=SingleCharToBinary(szTmp8j)*pow(2.0,7-j);if(bType)charbuffer2=0;itoa(iCh,buffer,16);/iftheintegerlessthan16,insertazerotobufferif(iCh16)charcTmp=buffer0;buffer0=0;buffer1=cTmp;s_return+=buffer0;s_return+=buffer1;buffer0=0;elsecharch=(char)iCh;s_return+=ch;returns_return;voidyxyDES:

CreateSubKey(char*sz)charszTmpL28=0;charszTmpR28=0;charszCi28=0;charszDi28=0;memcpy(szTmpL

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1