DES加密實(shí)驗(yàn)報(bào)告_第1頁
DES加密實(shí)驗(yàn)報(bào)告_第2頁
DES加密實(shí)驗(yàn)報(bào)告_第3頁
DES加密實(shí)驗(yàn)報(bào)告_第4頁
DES加密實(shí)驗(yàn)報(bào)告_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

《網(wǎng)絡(luò)安全技術(shù)》實(shí)驗(yàn)姓名系別實(shí)驗(yàn)地點(diǎn)學(xué)號年級班實(shí)驗(yàn)時間成績評定教師簽字實(shí)驗(yàn)項(xiàng)目DES加密解密程序設(shè)計(jì)與實(shí)現(xiàn)實(shí)驗(yàn)?zāi)康?理解DES加密與解密的程序設(shè)計(jì)算法思想編寫DES加密與解密程序,實(shí)現(xiàn)對明文的加密與解密,加深對數(shù)據(jù)加密與解密的理解,掌握DES加密算法思想,提高網(wǎng)絡(luò)安全的編程能力。所用軟件:TC、VC等其它編程工具實(shí)現(xiàn)實(shí)驗(yàn)內(nèi)容:掌握DES算法,編寫DES算法。1)初始置換 初始置換在第一輪運(yùn)算之前執(zhí)行,對輸入分組實(shí)施如下表所示的變換。此表應(yīng)從左向右、從上向下讀。例如,初始置換把明文的第58位換到第1位的位置,把第50位換到第2位的位置,把第42位換到第3位的位置,等等。58504234261810260524436282012462544638302214664564840322416857494133251791595143352719113615345372921135635547393123157在將這64位數(shù)據(jù)分為左右兩部分,每部分分別為32位,將左32位留下,將右32位按照下表進(jìn)行排列32123454567898910111213121314151617161718192021202122232425242526272829282930313212)密鑰置換 一開始,由于不考慮每個字節(jié)的第8位,DES的密鑰由64位減至56位,如下表所示。每個字節(jié)第8位可作為奇偶校驗(yàn)位以確保密鑰不發(fā)生錯誤。57494133251791585042342618102595143352719113605244366355473931231576254463830221466153453729211352820124接著,56位密鑰被分成兩部分,每部分28位。然后,根據(jù)輪數(shù),這兩部分分別循環(huán)左移l位或2位。下表給出了每輪移動的位數(shù)。輪12345678910111213141516位數(shù)1122222212222221在DES的每一輪中,從56位密鑰選出以下表中的48位子密鑰(SubKey),這些子密鑰Ki由下面的方式確定。14171124153281562110231912426816727201324152313747553040514533484449395634534642503629323)S盒置換當(dāng)產(chǎn)生了48位密鑰后就可以和右邊32位明文進(jìn)行異或運(yùn)算了,得到48位的密文。再經(jīng)過下論的S盒跌帶,其功能是把6bit數(shù)據(jù)變?yōu)?bit數(shù)據(jù),每個S盒是一個4行、16列的表。盒中的每一項(xiàng)都是一個4位的數(shù)。S盒的6個位輸入確定了其對應(yīng)的輸出在哪一行哪一列。下面列出所有的8個S盒。14413121511831061259070157414213110612119538411481362111512973105015128249175113141006131151814611349721312051031347152814120110691150147111041315812693215138101315421167120514921009146315511312711428137093461028514121151136498153011121251014711013069874151431152123713143069101285111241513811565034721211014910690121713151314528431506101389451112721442124171011685315130149141121247131501510398642111101378159125630141812711421361509104535121101592680133414751110154271295611314011389141552812370410113116432129515101114176081364112141508133129751061130117491101435122158614111312371410156805926111381410795015142312713284615111109314501271151381037412561101492711419121420610131535821147410813151290356118假定將S盒的6位的輸入標(biāo)記為b1、b2、b3、b4、b5、b6。則b1和b6組合構(gòu)成了一個2位的數(shù),從0到3,它對應(yīng)著表中的一行。從b2到b5構(gòu)成了一個4位的數(shù),從0到15,對應(yīng)著表中的一列。 例如,假設(shè)第6個S盒的輸入(即異或函數(shù)的第31位到36位)為110011。第1位和最后一位組合形成了11,它對應(yīng)著第6個S盒的第三行。中間的4位組合在一起形成了1001,它對應(yīng)著同一個S盒的第9列。S盒6的第三行第9列處的數(shù)是14(記住,行、列的記數(shù)均從0開始而不是從1開始),則值1110就代替了110011。4)P盒置換 S盒代替運(yùn)算后的32位輸出依照P盒進(jìn)行置換。該置換把每輸入位映射到輸出位,任意一位不能被映射兩次,也不能被略去,這個置換叫做直接置換。下表給出了每位移到的位置。16720212912281711523265183110282414322739191330622114255)再次異或運(yùn)算最后,將P盒置換的結(jié)果與最初的64位分組的左半部分異或,然后左、右半部分交換,接著開始另一輪。6)當(dāng)進(jìn)行到16論后,最終進(jìn)行一次末置換,形成密文40848165624643239747155523633138646145422623037545135321612936444125220602835343115119592734242105018582633141949175725實(shí)驗(yàn)步驟:usingSystem;usingSystem.Collections.Generic;usingSystem.Windows.Forms;usingSystem.Text;namespacedes{classDes{staticreadonlybyte[]BitIP={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,56,48,40,32,24,16,8,0,58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6};staticreadonlybyte[]BitCP={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,32,0,40,8,48,16,56,24};staticreadonlyint[]BitExp={31,0,1,2,3,4,3,4,5,6,7,8,7,8,9,10,11,12,11,12,13,14,15,16,15,16,17,18,19,20,19,20,21,22,23,24,23,24,25,26,27,28,27,28,29,30,31,0};staticreadonlybyte[]BitPM={15,6,19,20,28,11,27,16,0,14,22,25,4,17,30,9,1,7,23,13,31,26,2,8,18,12,29,5,21,10,3,24};staticreadonlybyte[,]sBox={{14,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},{15,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},{10,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},{7,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},{2,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},{12,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},{4,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},{13,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}};staticreadonlybyte[]BitPMC1={56,48,40,32,24,16,8,0,57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,60,52,44,36,28,20,12,4,27,19,11,3};staticreadonlybyte[]BitPMC2={13,16,10,23,0,4,2,27,14,5,20,9,22,18,11,3,25,7,15,6,26,19,12,1,40,51,30,36,46,54,29,39,50,44,32,47,43,48,38,55,33,52,45,41,49,35,28,31};enumTDesMode{dmEncry,dmDecry};byte[][]SubKey;publicDes(){SubKey=newbyte[16][];for(inti=0;i<SubKey.Length;i++){SubKey[i]=newbyte[6];}}voidinitPermutation(byte[]inData){byte[]newData=newbyte[8];for(inti=0;i<64;i++){if((inData[BitIP[i]>>3]&(1<<(7-(BitIP[i]&0x07))))!=0){newData[i>>3]|=(byte)(1<<(7-(i&0x07)));}}Array.Copy(newData,inData,8);}voidconversePermutation(byte[]inData){byte[]newData=newbyte[8];for(inti=0;i<64;i++){if((inData[BitCP[i]>>3]&(1<<(7-(BitCP[i]&0x07))))!=0){newData[i>>3]|=(byte)(1<<(7-(i&0x07)));}}Array.Copy(newData,inData,8);}voidexpand(byte[]inData,byte[]outData){Array.Clear(outData,0,6);for(inti=0;i<48;i++){if((inData[BitExp[i]>>3]&(1<<(7-(BitExp[i]&0x07))))!=0){outData[i>>3]|=(byte)(1<<(7-(i&0x07)));}}}voidpermutation(byte[]inData){byte[]newData=newbyte[4];for(inti=0;i<32;i++){if((inData[BitPM[i]>>3]&(1<<(7-(BitPM[i]&0x07))))!=0){newData[i>>3]|=(byte)(1<<(7-(i&0x07)));}}Array.Copy(newData,inData,4);}bytesi(bytes,byteinByte){intc=(inByte&0x20)|((inByte&0x1e)>>1)|((inByte&0x01)<<4);return(byte)(sBox[s,c]&0x0f);}voidpermutationChoose1(byte[]inData,byte[]outData){Array.Clear(outData,0,7);for(inti=0;i<56;i++){if((inData[BitPMC1[i]>>3]&(1<<(7-(BitPMC1[i]&0x07))))!=0){outData[i>>3]|=(byte)(1<<(7-(i&0x07)));}}}voidpermutationChoose2(byte[]inData,byte[]outData){Array.Clear(outData,0,6);for(inti=0;i<48;i++){if((inData[BitPMC2[i]>>3]&(1<<(7-(BitPMC2[i]&0x07))))!=0){outData[i>>3]|=(byte)(1<<(7-(i&0x07)));}}}voidcycleMove(byte[]inData,bytebitMove){for(inti=0;i<bitMove;i++){inData[0]=(byte)((inData[0]<<1)|(inData[1]>>7));inData[1]=(byte)((inData[1]<<1)|(inData[2]>>7));inData[2]=(byte)((inData[2]<<1)|(inData[3]>>7));inData[3]=(byte)((inData[3]<<1)|((inData[0]&0x10)>>4));inData[0]=(byte)(inData[0]&0x0f);}}staticreadonlybyte[]bitDisplace={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};voidmakeKey(byte[]inKey,byte[][]outKey){byte[]outData56=newbyte[7];byte[]key28l=newbyte[4];byte[]key28r=newbyte[4];byte[]key56o=newbyte[7];permutationChoose1(inKey,outData56);key28l[0]=(byte)(outData56[0]>>4);key28l[1]=(byte)((outData56[0]<<4)|(outData56[1]>>4));key28l[2]=(byte)((outData56[1]<<4)|(outData56[2]>>4));key28l[3]=(byte)((outData56[2]<<4)|(outData56[3]>>4));key28r[0]=(byte)(outData56[3]&0x0f);key28r[1]=(byte)(outData56[4]);key28r[2]=(byte)(outData56[5]);key28r[3]=(byte)(outData56[6]);for(inti=0;i<16;i++){cycleMove(key28l,bitDisplace[i]);cycleMove(key28r,bitDisplace[i]);key56o[0]=(byte)((key28l[0]<<4)|(key28l[1]>>4));key56o[1]=(byte)((key28l[1]<<4)|(key28l[2]>>4));key56o[2]=(byte)((key28l[2]<<4)|(key28l[3]>>4));key56o[3]=(byte)((key28l[3]<<4)|(key28r[0]));key56o[4]=(byte)(key28r[1]);key56o[5]=(byte)(key28r[2]);key56o[6]=(byte)(key28r[3]);permutationChoose2(key56o,outKey[i]);};}voidencry(byte[]inData,byte[]subKey,byte[]outData){byte[]outBuf=newbyte[6];byte[]buf=newbyte[8];expand(inData,outBuf);for(inti=0;i<6;i++)outBuf[i]=(byte)(outBuf[i]^subKey[i]);buf[0]=(byte)(outBuf[0]>>2);buf[1]=(byte)(((outBuf[0]&0x03)<<4)|(outBuf[1]>>4));buf[2]=(byte)(((outBuf[1]&0x0f)<<2)|(outBuf[2]>>6));buf[3]=(byte)(outBuf[2]&0x3f);buf[4]=(byte)(outBuf[3]>>2);buf[5]=(byte)(((outBuf[3]&0x03)<<4)|(outBuf[4]>>4));buf[6]=(byte)(((outBuf[4]&0x0f)<<2)|(outBuf[5]>>6));buf[7]=(byte)(outBuf[5]&0x3f);for(inti=0;i<8;i++)buf[i]=si((byte)i,buf[i]);for(inti=0;i<4;i++)outBuf[i]=(byte)((buf[i*2]<<4)|buf[i*2+1]);permutation(outBuf);for(inti=0;i<4;i++)outData[i]=outBuf[i];}voiddesData(TDesModedesMode,byte[]inData,byte[]outData){inti,j;byte[]temp=newbyte[4];byte[]buf=newbyte[4];for(i=0;i<8;i++)outData[i]=inData[i];initPermutation(outData);if(desMode==TDesMode.dmEncry){for(i=0;i<16;i++){for(j=0;j<4;j++)temp[j]=outData[j];for(j=0;j<4;j++)outData[j]=outData[j+4];encry(outData,SubKey[i],buf);for(j=0;j<4;j++)outData[j+4]=(byte)(temp[j]^buf[j]);};for(j=0;j<4;j++)temp[j]=outData[j+4];for(j=0;j<4;j++)outData[j+4]=outData[j];for(j=0;j<4;j++)outData[j]=temp[j];}elseif(desMode==TDesMode.dmDecry){for(i=15;i>=0;i--){for(j=0;j<4;j++)temp[j]=outData[j];for(j=0;j<4;j++)outData[j]=outData[j+4];encry(outData,SubKey[i],buf);for(j=0;j<4;j++)outData[j+4]=(byte)(temp[j]^buf[j]);};for(j=0;j<4;j++)temp[j]=outData[j+4];for(j=0;j<4;j++)outData[j+4]=outData[j];for(j=0;j<4;j++)outData[j]=temp[j];};conversePermutation(outData);}byte[]Redim(byte[]arr,intnewSize){if(newSize==arr.Length)returnarr;byte[]newArr=newbyte[newSize];Array.Copy(arr,0,newArr,0,Math.Min(arr.Length,newSize));returnnewArr;}publicbyte[]EncryBytes(byte[]inData,byte[]keyByte){byte[]tmpByte=newbyte[8];byte[]outByte=newbyte[8];if((inData.Length>0)&&(inData[inData.Length-1]==0)){}if(inData.Length%8!=0)inData=Redim(inData,(inData.Length+7)/8*8);if(keyByte.Length!=8)keyByte=Redim(keyByte,8);makeKey(keyByte,SubKey);byte[]outData=newbyte[inData.Length];for(inti=0;i<inData.Length/8;i++){for(intj=0;j<8;j++){tmpByte[j]=inData[i*8+j];}desData(TDesMode.dmEncry,tmpByte,outByte);for(intj=0;j<8;j++){outData[i*8+j]=outByte[j];}};returnoutData;}publicbyte[]DecryBytes(byte[]inData,byte[]keyByte){byte[]tmpByte=newbyte[8];byte[]outByt

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論