收藏 分享(赏)

DES加密解密纯C语言实现.doc

上传人:dreamzhangning 文档编号:4219331 上传时间:2018-12-16 格式:DOC 页数:22 大小:255.50KB
下载 相关 举报
DES加密解密纯C语言实现.doc_第1页
第1页 / 共22页
DES加密解密纯C语言实现.doc_第2页
第2页 / 共22页
DES加密解密纯C语言实现.doc_第3页
第3页 / 共22页
DES加密解密纯C语言实现.doc_第4页
第4页 / 共22页
DES加密解密纯C语言实现.doc_第5页
第5页 / 共22页
点击查看更多>>
资源描述

1、#include#include#includevoid show1() /主界面printf(“nnntt* DES 加密解密系统 *nn“);printf(“tt-n“);/printf(“tt-n“);printf(“tt*n“);printf(“tt*tttttt*n“);printf(“tt*tttttt*n“);printf(“tt*ttt1.加密ttt*n“);printf(“tt*tttttt*n“);printf(“tt*ttt2.解密ttt*n“);printf(“tt*tttttt*n“);printf(“tt*ttt3.退出ttt*n“);printf(“tt*tttt

2、tt*n“);printf(“tt*tttttt*n“);printf(“tt-n“);void show2() /加密界面printf(“nnntt* DES 加密 *nn“);printf(“tt-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“);pr

3、intf(“tt*tt3.退出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) /将字节转换成二进制流int i,k=0;for(i=0;i=1) if(j else bk+=0; int IP_Tabl

4、e64 = /初始置换(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, 2,60, 52, 44, 36, 28, 20, 12, 4,62, 54, 46, 38, 30, 22, 14, 6; int E_Table = /扩展变换 E31, 0, 1, 2, 3, 4,3, 4,

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

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

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

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

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

10、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;int IP_1_Table64 = /逆初始置换IP-1 39, 7, 47, 15, 55, 23, 63, 31,38, 6, 46, 14, 54, 22, 62, 30,37, 5,

11、 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_Table32 = /置换运算 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,

12、48,40,32,24,16,8, /密钥置换 PC_10,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_248 = /密钥置换 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,

13、48,38,55,33,52,45,41,49,35,28,31; void Replacement(int arry1,int arry2,int arry3,int num) /置换函数( 初始 IP,逆初始 IP,int i,tmp;for(i=0;i=0;j-)tempj=shai%2;shai=shai/2;for(j=0;j=0;j-)if(arryij=1)t=1;for(k=0;k=0;j-)if(arryij=1)t=1;for(k=0;k7-j;k+)t=t*2;temp+=t;bi=temp;void F_Function(int a32,int b32,int n) /

14、F 函数int i;int tmp48;int tep32;Replacement(a,E_Table,tmp,48); /扩展变换 E/*printf(“n 第%d 轮 E 盒扩展结果:“,n);for(i=0;i48;i+)if(i%8=0)printf(“ “);printf(“%d“,tmpi);*/for(i=0;i48;i+) /与子密钥异或tmpi = Kni;/*printf(“n 进入 S 盒的 48 比特:“);for(i=0;i48;i+)if(i%6=0)printf(“ “);printf(“%d“,tmpi);*/S_compress(tmp,tep); /压缩变换

15、 SReplacement(tep,P_Table,b,32); /置换运算 P/*printf(“n 第%d 次 P 置盒输出:“,l+);for(i=0;i32;i+)if(i%8=0)printf(“ “);printf(“%d“,bi);*/*printf(“nf%d的输出结果:“,n);for(i=0;i32;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)

16、; /初始置换 IP/*printf(“n 初始置换:“);for(i=0;i64;i+)if(i%8=0)printf(“ “);printf(“%d“,m1i);*/for(i=0;i32;i+)L0i=m1i;R0i=m1i+32;k=1;while(k17)F_Function(Rk-1,arry,k-1);for(i=0;i32;i+)Lki=Rk-1i;Rki=Lk-1iarryi;k+;for(i=0;i32;i+)c0i=R16i;c0i+32=L16i;Replacement(c0,IP_1_Table,c1,64); /逆初始置换void changeKey(int a16

17、48)int i,j;int tmp1648;for(i=0;i16;i+)for(j=0;j48;j+)tmpij=aij;for(i=0;i16;i+)for(j=0;j48;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;i16;i+)printf(“n“);for(j=0;j48;j+)if(j%8=0)printf(“ “);printf(“%d“,Kij);*/Replacement(c1,

18、IP_Table,c0,64); /初始 IPfor(i=0;i32;i+)L0i=c0i;R0i=c0i+32;k=1;while(k17)F_Function(Rk-1,arry,k-1);for(i=0;i32;i+)Lki=Rk-1i;Rki=Lk-1iarryi;k+;for(i=0;i32;i+)ti=R16i;ti+32=L16i;Replacement(t,IP_1_Table,m,64); /逆初始置换void print() /输出函数int i;int a12,b12,d316;int p64,q64;for(i=0;i32;i+)pi=L15i;p32+i=R15i;q

19、i=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;i12;i+)printf(“%x“,ai);printf(“t15 轮结果:“);for(i=0;i16;i+)printf(“%X“,d1i);printf(“ntt16 轮密钥:“);for(i=0;i12;i+)printf(“%X“,bi);printf(“t16 轮结果:“);for(i=0;i16;i+)printf(“%X“,d2i);prin

20、tf(“ntt 最后结果:“);for(i=0;i16;i+)printf(“%X“,d0i);/*printf(“n 最后结果二进制:“);for(i=0;i64;i+)printf(“%d“,ci);*/int main()int num,i,t;char s18,s28;int m64; /m 存放二进 制明文/ 密文int d64; /存放二进制密钥int s8;show1();printf(“ttinput you choice:“);while(1)scanf(“%d“,switch(num)case 1:system(“cls“); /调用清屏函数show2();scanf(“%

21、d“,if(num=1)while(strlen(s1)!=8)printf(“tt 请输入明文(8):“);scanf(“%s“,s1);To2Bin(s1,m); /将明文转换成二进制流while(strlen(s2)!=8)printf(“tt 请输入密钥(8):“);scanf(“%s“,s2);To2Bin(s2,d); /将密钥转换成二进制/*printf(“n 初始二进制明文:“);for(i=0;i64;i+)printf(“%d“,mi);*/*printf(“n 初始二进制密钥:“);for(i=0;i64;i+)printf(“%d“,di);*/SubKey(d);/*

22、printf(“n16 轮子密钥如下:“);for(i=0;i16;i+)printf(“nK%d:“,i+1);for(j=0;j48;j+)if(j%8=0)printf(“ “);printf(“%d“,Kij);*/Encryption(m,c);/*printf(“n16 次迭代左结果:“);for(i=0;i17;i+)printf(“nL%d:“,i);for(j=0;j32;j+)if(j%8=0)printf(“ “);printf(“%d“,Lij);printf(“n16 次迭代右结果:“);for(i=0;i17;i+)printf(“nR%d:“,i);for(j=0

23、;j32;j+)if(j%8=0)printf(“ “);printf(“%d“,Rij);*/print();/*printf(“n 二进制密文:“);for(i=0;i64;i+)printf(“%d“,ci);*/printf(“ntt 按 0 将此密文解密 ,1 返回上一层,2 返回主界面,其他键退出.“);scanf(“%d“,if(t=0)int s8;int a64;Decryption(c,a);/*printf(“n 解密后的二进制:“);for(i=0;i64;i+)printf(“%d“,ai);*/To102(a,s,64);printf(“ntt 解密结果:“);fo

24、r(i=0;i8;i+)printf(“%c“,si);printf(“ntt 按 1 加密,2 解密,3 退出“);else 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);To2

25、Bin(s2,d);SubKey(d);Encryption(m,c);print();printf(“ntt 按 0 将此密文解密 ,1 将密文存入文件,2 返回主界面,其他键退出.“);scanf(“%d“,if(t=0)int a64;Decryption(c,a);/*printf(“n 解密后的二进制:“);for(i=0;i64;i+)printf(“%d“,ai);*/To102(a,s,64);printf(“ntt 解密结果:“);for(i=0;i8;i+)printf(“%c“,si);printf(“ntt 按 1 加密,2 解密,3 退出“);else if(t=1)

26、int a16;char str30;FILE *fw;printf(“ntt 请输入文件名:“);scanf(“%s“,str);fw=fopen(str,“w“);if(fw=NULL)printf(“ntt 文件打开失败!n“);elseTo10(c,a,64);for(i=0;i16;i+)fprintf(fw,“%X“,ai);fclose(fw);printf(“ntt 密文保存成功!按 1 加密,2 解密,3 退出“);else if(t=2)system(“cls“);show1();elseexit(0);else if(num=3)system(“cls“);exit(0)

27、;elseprintf(“ntt 输入不正确,请重新输入:“);break;case 2:system(“cls“);printf(“nntt-DES 解密 -“);while(strlen(s1)!=8)printf(“nnntt 请输入密文 (8):“);scanf(“%s“,s1);To2Bin(s1,m);while(strlen(s2)!=8)printf(“tt 请输入密钥(8):“);scanf(“%s“,s2);To2Bin(s2,d);SubKey(d);Decryption(m,c);print();printf(“ntt 按 1 返回上一层,2 返回主界面,其他键退出.“

28、);scanf(“%d“,if(t=1)system(“cls“);printf(“nntt-DES 解密-“);while(strlen(s1)!=8)printf(“nntt 请输入密 文(8):“);scanf(“%s“,s1);To2Bin(s1,m);while(strlen(s2)!=8)printf(“tt 请输入密钥(8):“);scanf(“%s“,s);To2Bin(s2,d);SubKey(d);Decryption(m,c);print();else if(t=2)system(“cls“);show1();elseexit(0);break;case 3:system(“cls“);exit(0);default:printf(“输入不正确,请重新输入:“);return 0;说明:程序中有大部分的注释代码,去掉注释可查看各部分中间结果,如:十六轮子密钥,十六轮结果等程序运行主界面:加密界面:运行结果一:运行结果二:

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

当前位置:首页 > 网络科技 > C/C++资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报