DES加密解密純C語言實現(xiàn)_第1頁
DES加密解密純C語言實現(xiàn)_第2頁
DES加密解密純C語言實現(xiàn)_第3頁
DES加密解密純C語言實現(xiàn)_第4頁
DES加密解密純C語言實現(xiàn)_第5頁
已閱讀5頁,還剩17頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、#include<stdio.h>#include<stdlib.h>#include<string.h>void show1() /主界面printf("nnntt* DES加密解密系統(tǒng) *nn");printf("tt-n");/printf("tt-n");printf("tt*n");printf("tt*tttttt*n");printf("tt*tttttt*n");printf("tt*ttt1.加密ttt*n&qu

2、ot;);printf("tt*tttttt*n");printf("tt*ttt2.解密ttt*n");printf("tt*tttttt*n");printf("tt*ttt3.退出ttt*n");printf("tt*tttttt*n");printf("tt*tttttt*n");printf("tt-n");void show2()/加密界面printf("nnntt* DES加密 *nn");printf("tt-

3、n");printf("tt*n");printf("tt*tttttt*n");printf("tt*t請選擇明文和密鑰的輸入方式:tt*n");printf("tt*tttttt*n");printf("tt*tt1.直接輸入ttt*n");printf("tt*tttttt*n");printf("tt*tt2.從文件讀取ttt*n");printf("tt*tttttt*n");printf("tt*tt3.

4、退出tttt*n");printf("tt*tttttt*n");printf("tt-n");printf("ttt選擇:");void reader(char str30,char s8) /讀取明文和密鑰FILE *fp;fp=fopen(str,"r");if(fp=NULL)printf("明文讀取失敗!n");elsefscanf(fp,"%s",s);fclose(fp);void To2Bin(char p8,int b64) /將字節(jié)轉換成二進制流

5、int i,k=0;for(i=0;i<8;i+) int j=0x80;for(;j;j>>=1) if(j&pi) bk+=1; else bk+=0; int IP_Table64 = /初始置換(IP) 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,

6、2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6; int E_Table = /擴展變換E 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; int S_Box8416 = /8個s盒14, 4,13, 1, 2,15,

7、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,

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,

9、 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,1

10、2, 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,

11、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,

12、9, 0, 3, 5, 6,11;int IP_1_Table64 =/逆初始置換IP-1 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;int P_Tab

13、le32 =/置換運算P15,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; int PC_156 = 56,48,40,32,24,16,8, /密鑰置換PC_1 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; int PC_2

14、48 =/密鑰置換PC_213,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; void Replacement(int arry1,int arry2,int arry3,int num)/置換函數(shù)(初始IP,逆初始IP,int i,tmp;for(i=0;i<num;i+)tmp=arry2i;arry3i=arry1tmp;int move_times16 = 1,

15、1,2,2,2,2,2,2,1,2,2,2,2,2,2,1; /對左移位的規(guī)定void lif_move(int arry1,int arry2,int n) /左移位實現(xiàn)函數(shù)int i;for(i=0;i<28;i+)arry2i=arry1(n+i)%28;int K1648;/存放16輪子密鑰int c64;/存放明文或密文int L1732,R1732; /存放加密過程中左右局部void SubKey(int K064)/子密鑰產(chǎn)生函數(shù)int i;int K156,K256;int C1728,D1728;Replacement(K0,PC_1,K1,56);/密鑰置換PC_1f

16、or(i=0;i<28;i+)/將PC_1輸出的56比特分為左右兩局部C0i=K1i;D0i=K1i+28;i=0;while(i<16)int j;lif_move(Ci,Ci+1,move_timesi);lif_move(Di,Di+1,move_timesi);for(j=0;j<28;j+)K2j=Ci+1j;K2j+28=Di+1j;Replacement(K2,PC_2,Ki,48);/密鑰置換PC_2i+;/*printf("n子密鑰生成過程中,左邊生成的值:");for(i=0;i<17;i+)int j;printf("

17、nC%d:",i);for(j=0;j<28;j+)if(j%7=0)printf(" ");printf("%d",Cij);printf("n子密鑰生成過程中,右邊生成的值:");for(i=0;i<17;i+)int j;printf("nD%d:",i);for(j=0;j<28;j+)if(j%7=0)printf(" ");printf("%d",Dij);*/void S_compress(int arry,int shc)/S盒壓縮

18、變換,其中數(shù)組shc存放經(jīng)過s盒的結果int h,l;/行,列int sha8; /存放經(jīng)過s盒的十進制結果int i,j;int temp4;for(i=0;i<8;i+) /s盒壓縮變換h=arry(1+(i*6)-1*2 + arry(6+(i*6)-1;l =arry(2+(i*6)-1*8 + arry(3+(i*6)-1*4 + arry(4+(i*6)-1*2 + arry(5+(i*6)-1; shai=S_Boxihl; for(i=0;i<8;i+) for(j=3;j>=0;j-) tempj=shai%2; shai=shai/2; for(j=0;j

19、<4;j+) shc4*i+j=tempj; /*printf("n第%d次s盒的輸出:",m+);for(i=0;i<32;i+)if(i%8=0)printf(" ");printf("%d",shci);*/void To10(int a, int b,int n)/二進制轉十進制int i,j;int temp;int arry164;for(i=0;i<n/4;i+)for(j=0;j<4;j+)arryij=a4*i+j;for (i=0;i<n/4;i+)temp=arryi0*8+arry

20、i1*4+arryi2*2+arryi3*1;/*for(j=3;j>=0;j-)if(arryij=1)t=1;for(k=0;k<3-j;k+)t=t*2;temp+=t;*/bi=temp;void To102(int a, int b,int n)/二進制轉十進制int i,j;int temp;int arry88;int t=1,k;for(i=0;i<n/8;i+)for(j=0;j<8;j+)arryij=a8*i+j;for (i=0;i<n/8;i+)temp=0;for(j=7;j>=0;j-)if(arryij=1)t=1;for(k

21、=0;k<7-j;k+)t=t*2;temp+=t;bi=temp;void F_Function(int a32,int b32,int n)/F函數(shù)int i;int tmp48;int tep32;Replacement(a,E_Table,tmp,48);/擴展變換E/*printf("n第%d輪E盒擴展結果:",n);for(i=0;i<48;i+)if(i%8=0)printf(" ");printf("%d",tmpi);*/for(i=0;i<48;i+)/與子密鑰異或tmpi = Kni;/*pri

22、ntf("n進入S盒的48比特:");for(i=0;i<48;i+)if(i%6=0)printf(" ");printf("%d",tmpi);*/S_compress(tmp,tep); /壓縮變換SReplacement(tep,P_Table,b,32); /置換運算P/*printf("n第%d次P置盒輸出:",l+);for(i=0;i<32;i+)if(i%8=0)printf(" ");printf("%d",bi);*/*printf(&quo

23、t;nf%d的輸出結果:",n);for(i=0;i<32;i+)if(i%8=0)printf(" ");printf("%d",bi);*/void Encryption(int m064,int c164)int i,k;int arry32;int c064,m164;Replacement(m0,IP_Table,m1,64); /初始置換IP/*printf("n初始置換:");for(i=0;i<64;i+)if(i%8=0)printf(" ");printf("%d

24、",m1i);*/for(i=0;i<32;i+)L0i=m1i;R0i=m1i+32;k=1;while(k<17)F_Function(Rk-1,arry,k-1);for(i=0;i<32;i+)Lki=Rk-1i;Rki=Lk-1iarryi;k+;for(i=0;i<32;i+)c0i=R16i;c0i+32=L16i;Replacement(c0,IP_1_Table,c1,64); /逆初始置換void changeKey(int a1648)int i,j;int tmp1648;for(i=0;i<16;i+)for(j=0;j<

25、48;j+)tmpij=aij;for(i=0;i<16;i+)for(j=0;j<48;j+)Kij=tmp15-ij;void Decryption(int c1,int m)int c064,t64;int i,k;int arry32;changeKey(K);/*printf("n交換后的密鑰:n");for(i=0;i<16;i+)printf("n");for(j=0;j<48;j+)if(j%8=0)printf(" ");printf("%d",Kij);*/Replace

26、ment(c1,IP_Table,c0,64);/初始IPfor(i=0;i<32;i+)L0i=c0i;R0i=c0i+32;k=1;while(k<17)F_Function(Rk-1,arry,k-1);for(i=0;i<32;i+)Lki=Rk-1i;Rki=Lk-1iarryi;k+;for(i=0;i<32;i+)ti=R16i;ti+32=L16i;Replacement(t,IP_1_Table,m,64); /逆初始置換void print() /輸出函數(shù)int i;int a12,b12,d316;int p64,q64;for(i=0;i<

27、32;i+)pi=L15i;p32+i=R15i;qi=R16i;qi+32=L16i;To10(K14,a,48);To10(K15,b,48);To10(c,d0,64);To10(p,d1,64);To10(q,d2,64);printf("ntt15輪密鑰:");for(i=0;i<12;i+)printf("%x",ai);printf("t15輪結果:");for(i=0;i<16;i+)printf("%X",d1i);printf("ntt16輪密鑰:");for(i

28、=0;i<12;i+)printf("%X",bi);printf("t16輪結果:");for(i=0;i<16;i+)printf("%X",d2i);printf("ntt最后結果:");for(i=0;i<16;i+)printf("%X",d0i);/*printf("n最后結果二進制:");for(i=0;i<64;i+)printf("%d",ci);*/int main()int num,i,t;char s18,s

29、28;int m64; /m存放二進制明文/密文int d64; /存放二進制密鑰int s8;show1();printf("ttinput you choice:");while(1)scanf("%d",&num);switch(num)case 1:system("cls");/調(diào)用清屏函數(shù)show2();scanf("%d",&num);if(num=1)while(strlen(s1)!=8)printf("tt請輸入明文(8):");scanf("%s&q

30、uot;,s1);To2Bin(s1,m);/將明文轉換成二進制流while(strlen(s2)!=8)printf("tt請輸入密鑰(8):");scanf("%s",s2);To2Bin(s2,d);/將密鑰轉換成二進制/*printf("n初始二進制明文:");for(i=0;i<64;i+)printf("%d",mi);*/*printf("n初始二進制密鑰:");for(i=0;i<64;i+)printf("%d",di);*/SubKey(d);

31、/*printf("n16輪子密鑰如下:");for(i=0;i<16;i+)printf("nK%d:",i+1);for(j=0;j<48;j+)if(j%8=0)printf(" ");printf("%d",Kij);*/Encryption(m,c);/*printf("n16次迭代左結果:");for(i=0;i<17;i+)printf("nL%d:",i);for(j=0;j<32;j+)if(j%8=0)printf(" &

32、quot;);printf("%d",Lij);printf("n16次迭代右結果:");for(i=0;i<17;i+)printf("nR%d:",i);for(j=0;j<32;j+)if(j%8=0)printf(" ");printf("%d",Rij);*/print();/*printf("n二進制密文:");for(i=0;i<64;i+)printf("%d",ci);*/printf("ntt按0將此密文解密

33、,1返回上一層,2返回主界面,其他鍵退出.");scanf("%d",&t);if(t=0)int s8;int a64;Decryption(c,a);/*printf("n解密后的二進制:");for(i=0;i<64;i+)printf("%d",ai);*/To102(a,s,64);printf("ntt解密結果:");for(i=0;i<8;i+)printf("%c",si);printf("ntt按1加密,2解密,3退出");el

34、se if(t=1)system("cls");show2();else if(t=2)system("cls");show1();elseexit(0);else if(num=2)char str120,str220;printf("tt請輸入明文文件名:");scanf("%s",str1);reader(str1,s1);To2Bin(s1,m);printf("tt請輸入密鑰文件名:");scanf("%s",str2);reader(str2,s2);To2Bin

35、(s2,d);SubKey(d);Encryption(m,c);print();printf("ntt按0將此密文解密,1將密文存入文件,2返回主界面,其他鍵退出.");scanf("%d",&t);if(t=0)int a64;Decryption(c,a);/*printf("n解密后的二進制:");for(i=0;i<64;i+)printf("%d",ai);*/To102(a,s,64);printf("ntt解密結果:");for(i=0;i<8;i+)printf("%c",si);printf("ntt按1加密,2解密,3退出");else if(t=1)int a16;char str30;FILE *fw;printf("ntt請輸入文件名:");scanf("%s",str);fw=fopen(s

溫馨提示

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

評論

0/150

提交評論