赞
踩
目录
DES(Data Encryption Standard),数据加密标准,于1977年被美国国家标准局采纳为联邦信息处理标准46,这个算法本身指的是数据加密算法(DEA)。DES采用的是64位分组长度和56位的密钥长度,其算法原理是基于Feistel进行的改进。虽然其安全性已被证明不再安全,但理解它的算法原理对于密码学的学习有重要帮助。
本篇博客基于计算机网络安全的实验题目进行撰写,主要介绍DES的加解密原理,以及给出用C语言实现单个分组加密的代码。
对于任意的加密方案,总有两个输入:明文和密钥。DES的明文长度为64位,密钥长度为56位(以64位输入,经算法处理自动生成56位密钥)
其加密过程大致如下图所示:
首先对输入的64位明文进行初始置换(基于初始置换表),打乱顺序,再经过16轮轮函数的处理,最后将左右的32位进行互换,送入逆初始置换表进行逆初始置换,得到加密后的结果。
轮函数的过程及轮密钥的生成如下图所示:
其中,密钥首先要经过置换选择1将64位转换为56位。
注:所有的置换表、S盒均有预定义。
首先将需要用到的置换表作为全局变量进行定义
- int ct[64];//加密结果
- int K[16][48];//轮密钥存储
- int IP[8][8]={{58,50,42,34,26,18,10,2},
- {60,52,44,36,28,20,12,4},
- {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}};//初始置换表
- int antiIP[8][8]={{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,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}};//逆初始置换表
- int E[48]={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,
- 24,25,26,27,28,29,
- 28,29,30,31,32,1};//扩展置换表
- int P[32]={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};//置换函数
- int PC1[56]={57,49,41,33,25,17,9,
- 1,58,50,42,32,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,45,37,29,
- 21,13,5,28,20,12,4};//置换选择1
- int PC2[48]={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};//置换选择2
- int LN[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};//循环左移次数
- int S[8][64]={{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}};//S盒
DES处理的是二进制数据,人工输入的是八位字符,因此需要将输入按照ASCII进行二进制转换:
- int** str2hex(char* src) //字符转ASCII二进制
- {
- int t1[8];
- int i,j;
- int** bi = (int**)malloc(sizeof(int*)*8);
- for(i=0;i<8;i++)
- {
- t1[i]=src[i]+0;
- }
- for(i=0;i<8;i++)
- {
- bi[i] = (int*)malloc(sizeof(int) * 8);
- for(j=7;j>=0;j--)
- {
- bi[i][j]=t1[i]%2;
- t1[i]=t1[i]/2;
- }
- }
- return bi;
- }
对输入的64位(8位字符经过二进制转化来的)密钥进行处理,得到16个轮密钥:
- void KEY(int* keys) //密钥生成
- {
- int k0[56],tl[28],tr[28],temp;
- int i,j,k;
- for(i=0;i<56;i++) //置换选择1
- {
- k0[i]=keys[PC1[i]-1];
- }
- /*printf("\n置换选择1:");
- for(i=0;i<56;i++)
- {
- printf("%d",k0[i]);
- }*/
- for(i=0;i<28;i++) //左右分裂
- {
- tl[i]=k0[i];
- tr[i]=k0[i+28];
- }
- for(i=0;i<16;i++)
- {
- for(j=0;j<LN[i];j++) //循环左移
- {
- temp=tl[0];
- for(k=0;k<27;k++)
- {
- tl[k]=tl[k+1];
- }
- tl[27]=temp;
- temp=tr[0];
- for(k=0;k<27;k++)
- {
- tr[k]=tr[k+1];
- }
- tr[27]=temp;
- }
- for(j=0;j<28;j++)
- {
- k0[j]=tl[j];
- k0[j+28]=tr[j];
- }
- for(j=0;j<48;j++)//置换选择2
- {
- K[i][j]=k0[PC2[j]-1];
- }
- /*printf("\n第%d个轮密钥:",i+1);
- for(j=0;j<48;j++)
- {
- printf("%d",K[i][j]);
- if((j+1)%8==0)
- {
- printf(" ");
- }
- }*/
- }
- return;
- }
加密算法的输出是64位二进制,需要把他转换为8位十进制数,以ASCII扩展表进行输出:
- int* bi2c(int* c) //结果显示
- {
- int i;
- int* cp=(int*)malloc(sizeof(int)*8);
- for(i=0;i<8;i++)
- {
- cp[i]=c[i*8+0]*128+c[i*8+1]*64+c[i*8+2]*32+c[i*8+3]*16+c[i*8+4]*8+c[i*8+5]*4+c[i*8+6]*2+c[i*8+7]*1;
- }
- return cp;
- }
注意:经过加密后的字符ASCII值可能超过了127,因此需要用到ASCII扩展字符集,而正常的控制台无法输出,需要用到字符集转换函数:
- #include<windows.h>
-
- SetConsoleOutputCP(437);//为了输出特殊字符集
-
- SetConsoleOutputCP(936);//为了输出中文
字符一经转变则无法输出中文,因此输出密文后需将字符集再转回utf-8
- printf("加密系统:\n");
- char p[8],kw[8];
- int** phex = (int**)malloc(sizeof(int*)*8);
- int** kw2 = (int**)malloc(sizeof(int*)*8);
- int i,j,k, pb[64], pe[64], ape[64], keys[64];
- int* cyp=(int*)malloc(sizeof(int)*8);
- printf("请输入八位明文:");
- scanf("%s",p);
- printf("请输入八位密钥:");
- scanf("%s",kw);
- printf("即将加密的明文为:%s\n",p);
- //明文转二进制
- phex=str2hex(p);
- printf("二进制明文为:");
- k=0;
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- pb[k]=phex[i][j];
- k++;
- }
- }
- for(k=0;k<64;k++)
- {
- printf("%d",pb[k]);
- if((k+1)%8==0)
- {
- printf(" ");
- }
- }
- //初始置换
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- pe[i*8+j]=pb[IP[i][j]-1];
- }
- }
- printf("\n初始置换:");
- for(k=0;k<64;k++)
- {
- printf("%d",pe[k]);
- if((k+1)%8==0)
- {
- printf(" ");
- }
- }
- //左右分裂
- for(j=0;j<32;j++)
- {
- cl[j]=pe[j];
- cr[j]=pe[j+32];
- }
- for(j=0;j<48;j++)//扩展置换
- {
- ce[j]=cr[E[j]-1];
- }
- for(j=0;j<48;j++)//异或
- {
- ce[j]=(ce[j]+K[i][j])%2;
- }
- for(j=0;j<8;j++)//S盒代替
- {
- t2[0]=ce[j*6];
- t2[1]=ce[j*6+5];
- t4[0]=ce[j*6+1];
- t4[1]=ce[j*6+2];
- t4[2]=ce[j*6+3];
- t4[3]=ce[j*6+4];
- cs[j]=S[j][(t2[0]*2+t2[1]*1)*16+(t4[0]*8+t4[1]*4+t4[2]*2+t4[3]*1)];
- for(k=3;k>=0;k--)
- {
- temp[k]=cs[j]%2;
- cs[j]/=2;
- }
- for(k=0;k<4;k++)
- {
- pf[k+j*4]=temp[k];
- //printf("%d",pf[k+j*4]);
- }
- //printf("\t");
- }
- for(j=0;j<32;j++)//P置换备份
- {
- pf2[j]=pf[j];
- }
- for(j=0;j<32;j++)//P置换
- {
- pf[j]=pf2[P[j]-1];
- }
- for(j=0;j<32;j++)//左右异或
- {
- pf[j]=(cl[j]+pf[j])%2;
- }
- for(j=0;j<32;j++)
- {
- cl[j]=cr[j];
- cr[j]=pf[j];
- }
- printf("\n第%d轮加密结果:",i+1);
- for(j=0;j<32;j++)
- {
- printf("%d",cl[j]);
- if((j+1)%8==0)
- {
- printf(" ");
- }
- }
- for(j=0;j<32;j++)
- {
- printf("%d",cr[j]);
- if((j+1)%8==0)
- {
- printf(" ");
- }
- }
- //左右置换
- for(i=0;i<32;i++)
- {
- ape[i]=cr[i];
- ape[i+32]=cl[i];
- }
- //逆初始置换
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- ct[i*8+j]=ape[antiIP[i][j]-1];
- }
- }
- printf("\n逆初始置换:");
- for(k=0;k<64;k++)
- {
- printf("%d",ct[k]);
- if((k+1)%8==0)
- {
- printf(" ");
- }
- }
-
- printf("\n加密结果:");
- SetConsoleOutputCP(437);//为了输出特殊字符集
- cyp=bi2c(ct);
- for(i=0;i<8;i++)
- {
- printf("%c ",(unsigned char)cyp[i]);
- }
- return;
DES由于是基于Feistel的,因此其加解密算法完全一样,只需将加密后的二进制密文作为输入,在进行一次加密即可得到解密结果,注意,解密时轮密钥应倒着使用!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<math.h>
- #include<windows.h>
- #include<time.h>
- //函数声明
- void en_des();
- int** str2hex(char* src);
- void KEY(int* keys);
- int* bi2c(int* c);
- void de_des();
- //全局变量
- int ct[64];
- int K[16][48];
- int IP[8][8]={{58,50,42,34,26,18,10,2},
- {60,52,44,36,28,20,12,4},
- {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}};//初始置换表
- int antiIP[8][8]={{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,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}};//逆初始置换表
- int E[48]={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,
- 24,25,26,27,28,29,
- 28,29,30,31,32,1};//扩展置换表
- int P[32]={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};//置换函数
- int PC1[56]={57,49,41,33,25,17,9,
- 1,58,50,42,32,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,45,37,29,
- 21,13,5,28,20,12,4};//置换选择1
- int PC2[48]={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};//置换选择2
- int LN[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};//循环左移次数
- int S[8][64]={{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}};//S盒
-
- int main(void)
- {
- //加密
- en_des();
- //解密
- de_des();
-
- return 0;
- }
-
- //加密
- void en_des()
- {
- printf("加密系统:\n");
- char p[8],kw[8];
- int** phex = (int**)malloc(sizeof(int*)*8);
- int** kw2 = (int**)malloc(sizeof(int*)*8);
- int i,j,k, pb[64], pe[64], ape[64], keys[64];
- int* cyp=(int*)malloc(sizeof(int)*8);
- printf("请输入八位明文:");
- scanf("%s",p);
- printf("请输入八位密钥:");
- scanf("%s",kw);
- printf("即将加密的明文为:%s\n",p);
- //明文转二进制
- phex=str2hex(p);
- printf("二进制明文为:");
- k=0;
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- pb[k]=phex[i][j];
- k++;
- }
- }
- for(k=0;k<64;k++)
- {
- printf("%d",pb[k]);
- if((k+1)%8==0)
- {
- printf(" ");
- }
- }
- //初始置换
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- pe[i*8+j]=pb[IP[i][j]-1];
- }
- }
- printf("\n初始置换:");
- for(k=0;k<64;k++)
- {
- printf("%d",pe[k]);
- if((k+1)%8==0)
- {
- printf(" ");
- }
- }
- //密钥词转二进制
- kw2=str2hex(kw);
- k=0;
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- keys[k]=kw2[i][j];
- k++;
- }
- }
- //密钥生成
- KEY(keys);
- int cl[32],cr[32];
- //左右分裂
- for(j=0;j<32;j++)
- {
- cl[j]=pe[j];
- cr[j]=pe[j+32];
- }
- //轮函数
- for(i=0;i<16;i++)
- {
- int ce[48],cs[8],temp[4],pf[32],pf2[32];
- int t2[2],t4[4];
- for(j=0;j<48;j++)//扩展置换
- {
- ce[j]=cr[E[j]-1];
- }
- for(j=0;j<48;j++)//异或
- {
- ce[j]=(ce[j]+K[i][j])%2;
- //printf("%d",ce[j]);
- }
- for(j=0;j<8;j++)//S盒代替
- {
- t2[0]=ce[j*6];
- t2[1]=ce[j*6+5];
- t4[0]=ce[j*6+1];
- t4[1]=ce[j*6+2];
- t4[2]=ce[j*6+3];
- t4[3]=ce[j*6+4];
- cs[j]=S[j][(t2[0]*2+t2[1]*1)*16+(t4[0]*8+t4[1]*4+t4[2]*2+t4[3]*1)];
- for(k=3;k>=0;k--)
- {
- temp[k]=cs[j]%2;
- cs[j]/=2;
- }
- for(k=0;k<4;k++)
- {
- pf[k+j*4]=temp[k];
- //printf("%d",pf[k+j*4]);
- }
- //printf("\t");
- }
- for(j=0;j<32;j++)//P置换备份
- {
- pf2[j]=pf[j];
- }
- //printf("\n\n");
- for(j=0;j<32;j++)//P置换
- {
- pf[j]=pf2[P[j]-1];
- //printf("%d",pf[j]);
- }
- //printf("\n\n");
- for(j=0;j<32;j++)//左右异或
- {
- pf[j]=(cl[j]+pf[j])%2;
- }
- for(j=0;j<32;j++)
- {
- cl[j]=cr[j];
- cr[j]=pf[j];
- }
- printf("\n第%d轮加密结果:",i+1);
- for(j=0;j<32;j++)
- {
- printf("%d",cl[j]);
- if((j+1)%8==0)
- {
- printf(" ");
- }
- }
- for(j=0;j<32;j++)
- {
- printf("%d",cr[j]);
- if((j+1)%8==0)
- {
- printf(" ");
- }
- }
- }
- //左右置换
- for(i=0;i<32;i++)
- {
- ape[i]=cr[i];
- ape[i+32]=cl[i];
- }
- //逆初始置换
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- ct[i*8+j]=ape[antiIP[i][j]-1];
- }
- }
- printf("\n逆初始置换:");
- for(k=0;k<64;k++)
- {
- printf("%d",ct[k]);
- if((k+1)%8==0)
- {
- printf(" ");
- }
- }
-
- printf("\n加密结果:");
- SetConsoleOutputCP(437);//为了输出特殊字符集
- cyp=bi2c(ct);
- for(i=0;i<8;i++)
- {
- printf("%c ",(unsigned char)cyp[i]);
- }
- return;
- }
-
- int** str2hex(char* src) //字符转ASCII二进制
- {
- int t1[8];
- int i,j;
- int** bi = (int**)malloc(sizeof(int*)*8);
- for(i=0;i<8;i++)
- {
- t1[i]=src[i]+0;
- }
- for(i=0;i<8;i++)
- {
- bi[i] = (int*)malloc(sizeof(int) * 8);
- for(j=7;j>=0;j--)
- {
- bi[i][j]=t1[i]%2;
- t1[i]=t1[i]/2;
- }
- }
- return bi;
- }
-
- void KEY(int* keys) //密钥生成
- {
- int k0[56],tl[28],tr[28],temp;
- int i,j,k;
- for(i=0;i<56;i++) //置换选择1
- {
- k0[i]=keys[PC1[i]-1];
- }
- for(i=0;i<28;i++) //左右分裂
- {
- tl[i]=k0[i];
- tr[i]=k0[i+28];
- }
- for(i=0;i<16;i++)
- {
- for(j=0;j<LN[i];j++) //循环左移
- {
- temp=tl[0];
- for(k=0;k<27;k++)
- {
- tl[k]=tl[k+1];
- }
- tl[27]=temp;
- temp=tr[0];
- for(k=0;k<27;k++)
- {
- tr[k]=tr[k+1];
- }
- tr[27]=temp;
- }
- for(j=0;j<28;j++)
- {
- k0[j]=tl[j];
- k0[j+28]=tr[j];
- }
- for(j=0;j<48;j++)//置换选择2
- {
- K[i][j]=k0[PC2[j]-1];
- }
- }
- return;
- }
-
- int* bi2c(int* c) //结果显示
- {
- int i;
- int* cp=(int*)malloc(sizeof(int)*8);
- for(i=0;i<8;i++)
- {
- cp[i]=c[i*8+0]*128+c[i*8+1]*64+c[i*8+2]*32+c[i*8+3]*16+c[i*8+4]*8+c[i*8+5]*4+c[i*8+6]*2+c[i*8+7]*1;
- }
- return cp;
- }
-
- //解密
- void de_des()
- {
- SetConsoleOutputCP(936);
- printf("\n\n解密系统:");
- int ce[64],pp[64],dpt[64];
- int* dep=(int*)malloc(sizeof(int)*8);
- int i,j,k;
- //初始置换
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- ce[i*8+j]=ct[IP[i][j]-1];
- }
- }
- printf("\n初始置换:");
- for(k=0;k<64;k++)
- {
- printf("%d",ce[k]);
- if((k+1)%8==0)
- {
- printf(" ");
- }
- }
- int pl[32],pr[32];
- //左右分裂
- for(j=0;j<32;j++)
- {
- pl[j]=ce[j];
- pr[j]=ce[j+32];
- }
- //轮函数
- for(i=0;i<16;i++)
- {
- int pe[48],ps[8],temp[4],cf[32],cf2[32];
- int t2[2],t4[4];
- for(j=0;j<48;j++)//扩展置换
- {
- pe[j]=pr[E[j]-1];
- }
- for(j=0;j<48;j++)//异或
- {
- pe[j]=(pe[j]+K[15-i][j])%2;
- }
- for(j=0;j<8;j++)//S盒代替
- {
- t2[0]=pe[j*6];
- t2[1]=pe[j*6+5];
- t4[0]=pe[j*6+1];
- t4[1]=pe[j*6+2];
- t4[2]=pe[j*6+3];
- t4[3]=pe[j*6+4];
- ps[j]=S[j][(t2[0]*2+t2[1]*1)*16+(t4[0]*8+t4[1]*4+t4[2]*2+t4[3]*1)];
- for(k=3;k>=0;k--)
- {
- temp[k]=ps[j]%2;
- ps[j]/=2;
- }
- for(k=0;k<4;k++)
- {
- cf[k+j*4]=temp[k];
- }
- }
- for(j=0;j<32;j++)//P置换备份
- {
- cf2[j]=cf[j];
- }
- for(j=0;j<32;j++)//P置换
- {
- cf[j]=cf2[P[j]-1];
- }
- for(j=0;j<32;j++)//左右异或
- {
- cf[j]=(pl[j]+cf[j])%2;
- }
- for(j=0;j<32;j++)
- {
- pl[j]=pr[j];
- pr[j]=cf[j];
- }
- printf("\n第%d轮解密结果:",i+1);
- for(j=0;j<32;j++)
- {
- printf("%d",pl[j]);
- if((j+1)%8==0)
- {
- printf(" ");
- }
- }
- for(j=0;j<32;j++)
- {
- printf("%d",pr[j]);
- if((j+1)%8==0)
- {
- printf(" ");
- }
- }
- }
- //左右置换
- for(i=0;i<32;i++)
- {
- pp[i]=pr[i];
- pp[i+32]=pl[i];
- }
- //逆初始置换
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- dpt[i*8+j]=pp[antiIP[i][j]-1];
- }
- }
- printf("\n逆初始置换:");
- for(k=0;k<64;k++)
- {
- printf("%d",dpt[k]);
- if((k+1)%8==0)
- {
- printf(" ");
- }
- }
-
- printf("\n解密结果:");
- dep=bi2c(dpt);
- for(i=0;i<8;i++)
- {
- printf("%c",(unsigned char)dep[i]);
- }
- return;
- }
以上就是对DES加解密的简单实现。DES目前已不再安全,之后将介绍其衍生版本——AES。
(第一次写博客,请大家多多支持!有任何问题请在评论区指正,感谢大家!)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。