《2009252班 20093475 李行DES数据加密算法.docx》由会员分享,可在线阅读,更多相关《2009252班 20093475 李行DES数据加密算法.docx(28页珍藏版)》请在课桌文档上搜索。
1、上海电力学院实验报告课程名称.信息安全/计算机安全.实验项目实验二.DES数据加密舜法1.学号20093475班级20()9252当业值息安全同组人姓名指导教师姓名.贰为民.实验口期胆.I冬且月瀛月一、实验目的通过本实验的学习.深刻理解DES加密标准,提高算法设计能力,为今后继续学习密码技术和数字签名典定基础.二、实验内容根据DES加密标准,用C+设计编写符合DES髓法思想的加、解诙程序,能够实现时字符申和数组的加密和解密.三、实耽步1 .在操作系统环境卜启动VC+集成环境(MicrosoftVisua1.C+6.0,其中6.0为版本号,也可为其它版本),则产生如图I所示界面。图1VC+集成环
2、境界面2 .选择“文件”菜单下的“新建”命令,出现如图2所示界面(不可宜接按“新建”按钮.此按钮是新建一个文本文件).此界面缺省标签是要为新程序设定工程项目,但端轮小的源程序也可以不建立项目,可以百.接选择其左上用的“文件”标签,产生如图3所示界面。3 .在图3所示的界面中左边选定文件类里为“C+-SourceFi1.e,右边填好文件名并选定文件存放目录,然后单击“确定”按钿,出现如图4所示编程界面,开始输入程序。1.输入完源程序后,按.编译”菜单下的娘译命令,对源程序进行编译.系统将在下方的窗口中显示编译估息.如枭无此窗口,可按F1.t+2.键或执行查看菜单下的输Hr命令.如果编译后已无提示
3、错误则可按“编译“菜单下的构件命令来生成相应的可执行文件.随后可按编译菜单下的执行.命令运行的程序,图2新建VC+工程项目界面图3新建YC源程序文件界面图4YC+源程序编辑界面四、DES算法的过程1 .处理密钥I1.1 从用户处荻得64位密WKey.(每第8位为校脸位.为使密W有正确的奇偶校验.每个字节要为奇数个“1”位.1.2 处理过程:1.2.1 时密钥实旗变换,经过子密切换位表Pe-I的变换后,Key的位数由61位变成了56位。1.2.2 把变换后的密钥等分成两部分.曲28位记为CO.后28位记为D0.1.2.3 计算子密蜴(共16个),从i=1.开始.1.2.3.1分别对Ci-1、Di
4、T作循环左移来生成Ci、Di(共16次).1.2.3.2串联Ci、Di,得到一个56位数,然后对此数作子密钥换位表PC-2变换以产牛.48位子密钥Ki。1. 2.3.3按以上方法计算出16个子密的.2.对64位敷福块的处理:2. 1把数据分成64位的数据块,不城64位的以适当方式填补。2. 2对数据块利用初始变换IP表作变换”2. 3将变换后的数据块等分成前后两部分,前32位记为1.0,后32位记为R0.2. 4用16个子密的对数据加密.2. 4.1利用扩展祝换E,将32位R(i-D数据扩展成48位。2. 4.2用ER(卜1与子密钥K(i)作按位异成运算.2. 4.3把所得的48位数分成8个6
5、位数组.16位为Z1.712位为Z2,43-48位为Z82. 4.I从j=1.开始,用S食里的值自换Zj.S盒里的值为4位数,共8个S盒,输出32位数.2. 4.4.1取出Zj的第I和第6位申联起来成一个2位数,记为.m即是Sj盒里用来皆换Zj的数所在的行数.2. 4.4.2取出Zj的第2至第5位串联起来成一个4位数.记为n.n却是Sj盒里用来替换Zj的数所在的列数.2.4.九3用坐标(m,n)在S盒中查找出相应的值作为S食的输出,2. 4.5八个选择函数SjaWjW8)的输电拼接为32位;进制数据,把它作为P盘置换的输入,褥到输出2. 1.6把得到的结果与1.(i-1.)作异或运算.把计算结
6、果赋给R(i).2. 4.7把R(iT)的值St给1.(i.完成1轮乘积变换,2. 4.8从2.4.1起循环执行16次,直到K(16)也被用到。3. 5把R(16)和1.(16)顺序串联起来得到一个64位数.对这个数实版2.2变换的逆变换IP1.五.算法实现(1)源程序ttprg11aonce钵inc1.udenineIudeRinc1.udc&inc1.udeIiJangaesac*std:c1.ossBigInteger(IyPedefUnfdCdCbarbyte:pub1.ic:BigInteger(Void);BigInteeer(int6-1.va1.ue);BigInteer(un
7、signed_intMva1.ue);Big1.ntrgcr(constBig1.nt:BieIntegeroperator-O:BigIniejscrmxiPv(Bi#InIdgerx.Big1.nICgern);intbitCountO;BigIntegerBarrettRe=(BigIntegerbi2)return(bi2);)boo1.operatorMBigIn1.egorbi2;boo1.operator=(BiIntegerbi2):Big1.ntrgcroperator%(Big1.ntcgcrbi2):voidsing1.eByteDiviBigInteeerfcoutRe
8、11inder);voidIiuItIByuDivide(Big1.ntegerbi1.BigInteerftbi2,Big1.ntrgcr(MitQuticnt.Big1.n1.egerKnutRrnaindrr);intShiftRight(unsignedintbuffer,intbuf1.en,intshiftVa1.);BigIntegeroperator(intshiftVaD:Intshift1.ft(unsignedinibuffer).intbut1.en,intshifVa1.);boo1.operator;Bg1.nieeroperator+=(BiIrteerbi2):
9、Big1.11tr-gcrprrator/(Big1.n1.rgcrbi2);BigIntegeroperator-s(Big1.nteeerbi2);BigInteeeroperator-(BigIntegerbi2);siringUec1.o1.iex(unsireub1.ic:Big1.ntegcroperntr*(BigIntcgcrbi2);private:pub1.ic:xtnticconstintprinesBc1.v20O01J;/PrimSsnn1.Ierthan2000toIgStthegeneratedPrinOnurberintdata1.ength;/nun1.rof
10、actua1.CharSusedprivate:staticconstintrmx1.n.gth;/raxi11un1.engthoftheBig1.ntegerinUinI(4bytes/charethistosuittherequired1.eve1.ofprecision,unsignedint*dat;/storesbytesfrcotheBigInteger):CincIudeStdAfx.hUinCIUdC*BiInteer.h*constintBigInteger:Imax1.ength=512;constintBiR1.nI。RCr:Pr加。SBCIOB2000=101,103
11、,107.109.H3.10211031,1033.1039.1019.105h10611063,1069,1087,1091.1093,1097,739391910211211212345678913319979110210000111234$&789,171311970100oooO(M23456/8911103.1109.1117.1201.1213.1217,1301,1303,1307,1123tH29.1151.1223,1229.1231,1319,I3211.1327.1153.1163,1171.1237,1249,1259,1361,1367,1373,1181,1187.
12、1193.1277.1279.1283,1381,1399,1289.1291,1297,1409,1423,1427,1429,1433,1439,M47.1151.1453,1459,1471,1481,1-183.1487.1189.1*1511,1523,1531.154315191553,1559,1567,1571,1579,1583.1597,1601,1607.1609,161X1619,1621,1627,1637.1657,1663,1667.1669.1693,1697,1699.1709.1721,1723.1733,1741.1717.1753,1759,1777,1
13、783,1787.1789.1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879.1889,1901,1907,1913,1931,1933,1919,1951,1973,1979,1987.1993,1997,1999):Big1.nteger:BigInteger(void):data1.ength(0),data(0)Idata-newunsignedint三ax1.ength;nemset(data,0.max1.ength*sizeof(unsignedint);data1.ength=1;)BigInteger:BigInte
14、ger(_int6-1va1.ue)(data=newUnSiXnedintBax1.enRth;nemset(data,0.max1.engthsizeof(unsignedint);.int(ytempVaI=va1.ue;/copybytesfrom_int64toBigIntegerwithoutanyassumptionof/the1.engthofthe_int64datatypedata1.ength=0:whi1.e(va1.ue!=0&data1.ength0)/overf1.owcheckfor+veva1.ue(if(va1.ue!-0(data三ax1.ength1&0
15、x80000000)!-0)assert(fa1.se);)e1.seif(IeepVa1.0)/underf1.owcheckfor-veva1.ue(if(va1.ue!=-1(datadata1.-ength-1&0x80000000)=0)assert(fa1.se);)if(data1.ength=0)data1.ength=I;)BiInteger:BigIntcer(ursi11cdJnt61va1.ue)(data=neunsignedintaax1.ength;emset(data,O,max1.ength*sizeof(unsignedint);/copybytesfrom
16、u_in1.61.toBiJdn1.CROrwithoutanyassu三p1.ionofHthe1.engthoftheu_int64datatypedata1.ength=0:WhiIe(va1.ue!=0&data1.e11thmax1.ength)(datadata1.engthj=(unsignedint)(va1.uekOXFFFFFFFF);va1.ue=32:data1.ength+;)if(va1.ue!=0(data(ax1.ength-1&0x80000000)!=0)assert(fa1.se);if(data1.ength=0)data1.ength=1;)BigIn
17、teger:Big1.nteger(constBig1.nteger&bi)(data=newUnSiKmxIin1.三ax1.e11th;nemset(data,0.max1.ength-sizeof(unsignedint);data1.ength=bi.data1.ength;for(inti=0;i=1imit;i-)(intPUSya1.=(int)va1.uei;if(posVa1.=WPOSYa1.=A&posVa1.=radix)assert(fa1.se);)e1.seif(va1.ue0=,)posVa1.三-posVa1.;resu1.t=resu1.t+(mu1.tip
18、1.ierBigInteger(_int64)pos1.);if(i1)=1.imit)mu1.tip1.ier=mu1.tip1.ierBigInteger(_int61.)radix);)if(va1.ue0=*-*)/negativeva1.ues(if(resu1.t.datamax1.c11g1.h-1&0x80000000)=0)assert(fa1.se);)e1.se/positiveva1.ues(if(resu1.t.datamax1.jengthIj&0x80000000)!=0)assert(fa1.se);)data=nounsignedintBax1.ength;c
19、mset(data,0,HgthaSiZeOr(UnSiRnCdin1.);for(inti-0:imax1.enth)assert(fa1.se);data=neunsignedintHax1.engthJ;emset(data,O,Ifiax1.ength*SiZCOr(UnSiRnCdint);for(inti=in1.en-1,j=O;i=3:i-=4,j+)(dataj=(unsignedint)(inDatai-324)+(inDatai-216)*(inDatai-18)inDatai);)if(1.eftover=1)datadata1.ength1三(unsignedint)
20、in1.)ata0;e1.seif(1.eft(hrer三=2)datadata1.ength-1=(unsignedint)(inDataO8)+inata1.);e1.seif(IeftOver=3)datadata1.ength1三(unsignedint)(inDatO16)(inData1.8)inData2);hiIe(data1.ength184datadata1.ength-1=O)data1.ength一:)BigIntegor:BieInteger(unsignedintinDataintin1.vn)data1.ength=in1.en;if(data1.Ongthmax
21、1.ength)assert(fa1.se);data:newUnSiRTWdint三ax1.ength;Bemset(data.0.max1.ength/sizeof(max1.ength):for(inti=da1.a1.ength-1,j=0:i=0:i,j+)dataj=inDatai;hiIe(da1.a1.ength1&AdataIdata1.ength-Ij=0)data1.enth-)BigIntegerBigInteger:!operator(Big1.ntegerbi2)(BigIntegerbi1(this);intIastPos=max1.ength-1;boo1.bi
22、INeg=fa1.se,bi2Neg=fa1.se;/taketheabso1.uteva1.ueoftheinputstry(if(this-dataIastPosA0x80000000)!=0)/bi1.neative(biINeg=true;bi1.=-bi1.;)if(bi2.data1.astPos&0x80000000);-0)/bi2negativebi2Neg=true;bi2=-bi2:)catch()BigIntegerresu1.t;/mu1.tip1.ytheabso1.uteva1.uestryIfor(inti=0:ibi1.data1.ength;i*)ifbi1
23、.datai=0)continue;unsigned_int61.mcarry=0:for(intj=0,k=i;jBaX1.eng1.h)resu1.t.data1.ength=max1.ength;IFhiIe(resu1.t.data1.ength1&resu1.t.dataresu1.t.data1.ength1二二O)resu1.t.data1.ength;/overf1.owcheck(resu1.tis-ve)if(resu1.t.data1.astPos&0x80000000)!-O)(if(biINeg!=1.i2Neg&resu1.t.data1.astPos=0x8000
24、0000)/differentsign/hand1.ethespecia1.casewhere11u1.tip1.icationproduces/anaxnegativenunborin2*scomp1.ement.if(resu1.t.data1.efth=1)returnresu1.t;e1.seboo1.isMaxNeg=true;for(inti=0:ii2)(if(ibi2=this)(return*this:Jif(data!=NU1.1.)(de1.etedata:data=NU1.1.;)data=nrunsignedintaax1.ength;cf1.iset(data,0,
25、max1.engthsizeof(unsignedint):data1.ength-gth;for(inti=0:idata1.c11thbi2.data1.ength)?this-data1.fith:th:_int64carry=0:for(inti=0:idatai(_int64)bi2.datai+carry:carry=sum32;resu1.t,(1.atai)=(unsignedint)(sumOXFFFFFFFF);if(carry!=O&resu1.t.1.ata1.en1.h1I1.&resu1.t.dataresu1.t.data1.ength-1三=O)resu1.t.
26、data1.ength;/overf1.owcheckintIastPos=max1.ength-1;if(this-data1.asiPos&080000000)(bi2.dataIastPos&0x80000000)&(resu1.t.data1.stPos&0x80000000)!二(this-dataIastPosJ&0x80000000)(assert(fa1.se);)returnresu1.t;)BigIntegerBigInteger:Zoperator()(/hand1.enegofzeroseparate1.ysinceit,11causeanoverf1.ow/ifwcp
27、roceed.if(this-data1.ength=1&this-data0=0)return-this;Big1.ntegerresu1.t(*this);/scoBp1.eeentfor(inii=0:idatai);/addonetoresu1.tof1,sco三(1.ement_int61.va1.,carry=1:intindex=0;hiIe(carry!=0&indexdata11ax1.ength-1&0x80000000)=(resu1.t.datagth-/throw(newArithmeticException(*0verf1.owinnegation,n*);resu
28、1.t,daIa1.ength=Dax1.ength:IrhiIR(resu1.t.data1.ength1I1.&resu1.t,dataresu1.t.data1.ength1.0)resu1.t.data1.ength;returnresu1.t;)BigIntegerBig1.nteger:modPow(Big1.ntegerexp.BigIntegern)(if(exp.datamax1.ength-10x80000000)!=0)(/throw(nowrithmetic1.ixception(*Positiveexponentson1.y.*);returnBig1.nteger(
29、_int64)0):)BigIntegerresu1.tNum(_int64)1);BigIntegertenpNum;boo1.thisNeative=fa1.se;if(this-datamax1.ength-140x80000000)!=0)/negativethis(tOinpNum=-(*this)n;thisNegative=true;)e1.setcmpNu=S1.hiS)%n:/ensures(tempNum*tempNu三)b(2k)if(n.datamax1.ength-1&0x80000000)!=0)/negativenn=-n;/ca1.cu1.ateconstant
30、=b(2k)/mBigIntegerconstant;inii=n.data1.crth1;constant.datai二0x00000001:constant.data1.ength=i*1;constant=constant/n;intIOIa1.Bi1.S=exp.bitCountO;intcount=O;/performsquaringandBu1.tip1.yexponentiationfor(intpos=O;posexp.data1.ength;pos+)(unsignedintmask;OxO1.;/Conso1.e.Write1.ine(*pos=*+pos);for(int
31、index=0:indexength=I&tenpNua.data0J=1)(if(thiSNegativeh&(exp.data0&OxD!=0)/oddexpreturnresuItNus;returnresu1.tNum;)count+;if(counttota1.Bits)break:)if(thisNegative&(exp.data&0x1.)!=0)/oddexpreturn-resu1.tNm;returnresu1.tNub;)intBigInteger:ZbitCbuntO(UhiIe(data1.ength1datadata1.ength-1=0)data1.ength一
32、;unsignedintva1.ue三datadata1.ength1;unsignedintBaSk=0x80000000;intbits=32;whi1.e(bitsOM(va1.ue&Sk)=O)bits;mask=1:)bits+=(data1.erth-1)5);returnbits;)BigIntegerBigInteger:BarrettReduction(BigIntegerx,BigIntegern.BigIntegerconstant)(intk=n.data1.ength,kP1.usOne=k+1kMinusOne=k1;BigIntegerq1.;/q1.=x/b(k
33、-1.)for(inti=kMinusOne,j=0;ix.data1.ength;i+,j+)q1.dataj=x.datai;q1.daia1.ength=x.daia1.ength-kMinusOne;if(q1.data1.ength=0)q1.data1.ength=1;BigIntegerq2-q1.constant;BigIntegerq3;/q3=q2/b(k1.)for(inti=kP1.usOne,j=0;iq2.data1.ength;i,。/)q3.dataj=q2.datai;(3.data1.ength=q2.data1.ength-kP1.usOne;if(q3.
34、da1.a1.x?ngthkP1.usOne)?kP1.usOne:x.data1.ength:for(inti=O:iar1.ia1.BUIIiP1.iCaIiOnofq3andnBigIntegerr2;for(inti=O:iq3.(Iata1.ength:i+)(if(q3.datai=O)continue;unsignedJntfr1.三carry=O;intt=i;for(intj-0:jn.data1.ength&4tkPIusOne;j*.t4)(/t=ijunsigned_in1.61va1.=(unsignedint64)q3.datai*(unsignedint61)n.dataj)+(unsignedint61.)r2.datatacarry:r2.datat=(unsignedint)(va1.&OxFFfTFFFF);mcarry=(va1.32);)if(t1&r2.datar2.data1.ength-1=O)r2.data1.ength;r1.-=r2;if(r1.dataax1.eneth-1&0x80000000)!=0)/negative(BigIntegerva1.:va1.datakP1.usne=0x00000001;va1.data1.ength=kP1.usOne*1:r1.+=va1.;)whi1.e(r1.=n)r1.-=n;