版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)課程 :網(wǎng)絡(luò)安全 實(shí)驗(yàn)項(xiàng)目 :DES加密算法的實(shí)現(xiàn) 實(shí)驗(yàn)日期 : 系:數(shù)學(xué)與計(jì)算機(jī)學(xué)院 班級(jí):網(wǎng)絡(luò)工程 姓名: 學(xué)號(hào): 指導(dǎo)教師: 成績(jī): 【實(shí)驗(yàn)?zāi)康摹?l掌握DES加密算法的基本原理l使用編程語(yǔ)言實(shí)現(xiàn)DES的加密和解密【實(shí)驗(yàn)環(huán)境】lWindows 系統(tǒng)lJava編程環(huán)境或者VC+編程環(huán)境【實(shí)驗(yàn)內(nèi)容和步驟】·第一步:變換明文。對(duì)給定的64位比特的明文x,首先通過(guò)一個(gè)置換IP表來(lái)重新排列x,從而構(gòu)造出64位比特的x0,x0=IP(x)=L0R0,其中L0表示x0的前32比特,R0表示x0的后32位。第二步:按照規(guī)則迭代。規(guī)則為L(zhǎng)i = Ri-1Ri = Lif(Ri-1,Ki) (
2、i=1,2,316)經(jīng)過(guò)第一步變換已經(jīng)得到L0和R0的值,其中符號(hào)表示的數(shù)學(xué)運(yùn)算是異或,f表示一種置換,由S盒置換構(gòu)成,Ki是一些由密鑰編排函數(shù)產(chǎn)生的比特塊。f和Ki將在后面介紹。第三步:對(duì)L16R16利用IP-1作逆置換,就得到了密文y。代碼如下:#include "stdio.h"#include "string.h"#define uchar unsigned char/*明文轉(zhuǎn)換聲明部分*/IP1置換表int IP_164=58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
3、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;/IP2逆置換表int IP_264=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,
4、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;/E擴(kuò)展置換表int E_case48=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,
5、24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1;/S盒壓縮int S1416=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; int S2416=15, 1, 8, 14,
6、 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;int S3416=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,
7、 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;int S4416= 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,
8、3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14;int S5416= 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;int S6416=12, 1, 10,
9、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;int S7416= 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,
10、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;int S8416=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
11、, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11;/P盒置換int Permute32= 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;/*字節(jié)與二進(jìn)制相互變換部分*/字節(jié)轉(zhuǎn)換成二進(jìn)制int ByteToBit(char ch,char bit8)uchar x;for(x=0;x<8;x+) *(bit+x)=(ch<<x)&a
12、mp;0x80)>>7; return 0;/字符串轉(zhuǎn)換成二進(jìn)制位串int Char8ToBit64(char ch8,char bit64)uchar x;for(x=0;x<8;x+)ByteToBit(*(ch+x),bit+(x<<3);return 0;/二進(jìn)制轉(zhuǎn)換成字節(jié)int BitToByte(char bit8,char *ch)uchar x;for(x=0;x<8;x+)*ch|=*(bit+x)<<(7-x);return 0;/將二進(jìn)制串轉(zhuǎn)換成字符串int Bit64ToChar8(char bit64,char ch8)
13、uchar x;memset(ch,0,8); /把ch8全部清零。for(x=0;x<8;x+)BitToByte(bit+(x<<3),ch+x);return 0;/*密鑰聲明部分*/密鑰PC-1置換表int PC_156=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
14、, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4; /密鑰PC-2置換表int PC_248=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;/密鑰每次輪回移位表uchar Left_Move16=1, 1, 2, 2, 2, 2, 2, 2, 1, 2
15、, 2, 2, 2, 2, 2, 1;/*密鑰的生成函數(shù)部分*/PC-1置換函數(shù)int PC1_permute(char pc164,char PC1_Buff56)uchar x;for(x=0;x<56;x+)PC1_Buffx=pc1PC_1x-1;return 0;/PC密鑰左移int KeyLeftMove(char pcB56,char Move)uchar x;char Cn28,Dn28;char a_buff,b_buff,c_buff,d_buff;/*PC-1密鑰56位分離成左右各28位* memcpy(Cn,pcB,28); memcpy(Dn,pcB+28,28
16、);/*PC密鑰左移部分*a_buff=Cn0;b_buff=Dn0; if(2=Move) c_buff=Cn1;d_buff=Dn1; for(x=0;x<28;x+)if(27=x)if(1=Move)Cn27=a_buff;Dn27=b_buff;if(2=Move)Cn26=a_buff;Cn27=c_buff;Dn26=b_buff;Dn27=d_buff;elseCnx=Cnx+Move;Dnx=Dnx+Move;/*28bit合成56bit* memcpy(pcB,Cn,28); memcpy(pcB+28,Dn,28);return 0;/PC-2逆置換函數(shù)int PC
17、2_permut(char pc256,char PC2_Buff48)uchar x;for(x=0;x<48;x+)PC2_Buffx=pc2PC_2x-1;return 0;/產(chǎn)生16輪的子密鑰函數(shù)int Key16Itera(char key64,char subKeys1648)uchar x,move;char temp156;PC1_permute(key,temp1);/PC1置換,返回值tempfor(x=0;x<16;x+)move=Left_Movex;KeyLeftMove(temp1,move);/移位,返回值temp,56bitPC2_permut(te
18、mp1,subKeysx);return 0;/*明文加密函數(shù)部分*/IP置換int IP1_permute(char IP164)uchar x;char IP1Buff64;for(x=0;x<64;x+)IP1Buffx=IP1IP_1x-1;memcpy(IP1,IP1Buff,64);return 0;/32bit經(jīng)E盒擴(kuò)展成48bitint R_Expand48bit(char R32,char RE48)uchar x;for(x=0;x<48;x+)REx=RE_casex-1;return 0;/IP64逆置換int IP2_permute(char IP264)
19、uchar x;char temp264;for(x=0;x<64;x+)temp2x=IP2IP_2x-1;memcpy(IP2,temp2,64);return 0;/經(jīng)P盒置換int Pcase(char S8Byte32)uchar x;char temp332;for(x=0;x<32;x+)temp3x=S8BytePermutex-1;memcpy(S8Byte,temp3,32);return 0; /經(jīng)S盒壓縮成32bitint Reduceto32bit(char EKey48,char S32bit32)uchar x,Rank,Line;int out;fo
20、r(x=0;x<8;x+) Rank=(EKey1+6*x<<3)|(EKey2+6*x<<2)|(EKey3+6*x<<1)|(EKey4+6*x); Line=(EKey6*x<<1)|(EKey5+6*x); /在S盒尋找需要的數(shù) if(0=x) out=S1LineRank; if(1=x) out=S2LineRank; if(2=x) out=S3LineRank; if(3=x) out=S4LineRank; if(4=x) out=S5LineRank; if(5=x) out=S6LineRank; if(6=x) ou
21、t=S7LineRank; if(7=x) out=S8LineRank; /把一位數(shù)拆成數(shù)四位數(shù) S32bit4*x+0=(char)(out)&0x08)>>3; S32bit4*x+1=(char)(out)&0x04)>>2; S32bit4*x+2=(char)(out)&0x02)>>1; S32bit4*x+3=(char)(out)&0x01);return 0; /*明文16次迭代,加密,*輸入值:mw64子密鑰:miyue1648*返回值:data64*/int Encryp(char data64,cha
22、r Zkey161648)uchar x,y;char Left32,Right32,EKbuff48;char SE32bit32;IP1_permute(data);/IP置換memcpy(Left,data,32);/分離32bitmemcpy(Right,data+32,32); for(x=0;x<16;x+)R_Expand48bit(Right,EKbuff);/E盒擴(kuò)展for(y=0;y<48;y+) /與密鑰異或EKbuffy=Zkey16xy; Reduceto32bit(EKbuff,SE32bit);/經(jīng)S盒壓縮32bit,返回值SE32bit Pcase(
23、SE32bit); /經(jīng)P盒置換,返回值SE32bit for(y=0;y<32;y+) /與左半部分Left 32bit異或SE32bity=Lefty;memcpy(Left,Right,32);memcpy(Right,SE32bit,32);memcpy(data,Right,32);/合成64bitmemcpy(data+32,Left,32);IP2_permute(data); /IP逆置換return 0;/*密文輸入,解密,16次迭代*輸入值:miwen64*輸出值:mw64*/int Decryp(char miwen64,char D_Zkey161648)ucha
24、r x,y;char D_Left32,D_Right32;char D_EKbuff48,D_SE32bit32;IP1_permute(miwen); /IP1置換。memcpy(D_Left,miwen,32);/分離32bitmemcpy(D_Right,miwen+32,32);for(x=16;x>0;x-) /16輪迭代R_Expand48bit(D_Right,D_EKbuff);/E盒擴(kuò)展for(y=0;y<48;y+) /與密鑰異或D_EKbuffy=D_Zkey16x-1y;Reduceto32bit(D_EKbuff,D_SE32bit);/經(jīng)S盒壓縮32b
25、it,返回值D_SE32bit。 Pcase(D_SE32bit); /經(jīng)P盒置換,返回值SE32bit for(y=0;y<32;y+) D_SE32bity=D_Lefty;memcpy(D_Left,D_Right,32);memcpy(D_Right,D_SE32bit,32);memcpy(miwen,D_Right,32); /合成64bitmemcpy(miwen+32,D_Left,32);IP2_permute(miwen); /IP逆置換return 0; /*主函數(shù)部分*/int main(void) uchar x; char MingW64bit64,MingW8Byte8; char Key64bit64,Key8Byte8; char Key161648,temp264; char Mybuff8; printf("請(qǐng)輸入密鑰:n"); scanf("%s",Key8Byte); Char8ToBit64(Key8Byte,Key64bit); /密鑰字符串置換成二進(jìn)制串 for(x=0;x<8;x+) /補(bǔ)償足8位奇偶校驗(yàn)位 memcpy(temp2+8*x,Key64bit+7*x,7); temp2x*8+7=1; memcpy(Key64bit,temp2,64); Key16Itera(K
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 帽粉產(chǎn)品供應(yīng)鏈分析
- 專業(yè)書(shū)籍出版行業(yè)相關(guān)項(xiàng)目經(jīng)營(yíng)管理報(bào)告
- 農(nóng)業(yè)智能施肥機(jī)行業(yè)經(jīng)營(yíng)分析報(bào)告
- 吸入器產(chǎn)品供應(yīng)鏈分析
- 農(nóng)業(yè)保險(xiǎn)科技行業(yè)市場(chǎng)調(diào)研分析報(bào)告
- 石蠟紙市場(chǎng)發(fā)展前景分析及供需格局研究預(yù)測(cè)報(bào)告
- 農(nóng)業(yè)生物農(nóng)藥行業(yè)市場(chǎng)調(diào)研分析報(bào)告
- 兩輪機(jī)動(dòng)車用擋泥板產(chǎn)業(yè)鏈招商引資的調(diào)研報(bào)告
- 手表表柄產(chǎn)業(yè)鏈招商引資的調(diào)研報(bào)告
- 頭發(fā)造型用噴霧產(chǎn)業(yè)鏈招商引資的調(diào)研報(bào)告
- 新版東方大學(xué)俄語(yǔ)1-第8課
- 【課件】第二單元第三節(jié)漢族民歌課件-2021-2022學(xué)年高中音樂(lè)人音版(2019)必修音樂(lè)鑒賞
- 急性腎損傷PPT通用課件
- 高標(biāo)準(zhǔn)基本農(nóng)田建設(shè)監(jiān)理工作總結(jié)
- 機(jī)電安裝工程技術(shù)專業(yè)培訓(xùn)
- 7逆合成分析法與合成路線設(shè)計(jì)
- 工程材料構(gòu)配件設(shè)備報(bào)審表
- 《Monsters 怪獸》中英對(duì)照歌詞
- 華東地區(qū)SMT公司信息
- 隧道棄渣及棄渣場(chǎng)處理方案
- 隔代教育PPT課件
評(píng)論
0/150
提交評(píng)論