1 #include "memory.h"
2 #include "stdio.h"
3 enum {encrypt,decrypt};//ENCRYPT:加密,DECRYPT:解密
4 void des_run(char out[8],char in[8],bool type=encrypt);
5 //设置密钥
6 void des_setkey(const char key[8]);
7 static void f_func(bool in[32],const bool ki[48]);//f函数
8 static void s_func(bool out[32],const bool in[48]);//s盒代替
9 //变换
10 static void transform(bool *out, bool *in, const char *table, int len);
11 static void xor(bool *ina, const bool *inb, int len);//异或
12 static void rotatel(bool *in, int len, int loop);//循环左移
13 //字节组转换成位组
14 static void bytetobit(bool *out,const char *in, int bits);
15 //位组转换成字节组
16 static void bittobyte(char *out, const bool *in, int bits);
17 //置换IP表
18 const static char ip_table[64]={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};
19 //逆置换IP-1表
20 const static char ipr_table[64]={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};
21 //E 位选择表
22 static const char e_table[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};
23 //P换位表
24 const static char p_table[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};
25 //pc1选位表
26 const static char pc1_table[56]={
27 57,49,41,33,25,17,9,1,
28 58,50,42,34,26,18,10,2,
29 59,51,43,35,27,19,11,3,
30 60,52,44,36,63,55,47,39,
31 31,23,15,7,62,54,46,38,
32 30,22,14,6,61,53,45,37,
33 29,21,13,5,28,20,12,4
34 };
35 //pc2选位表
36 const static char pc2_table[48]={
37 14,17,11,24,1,5,3,28,
38 15,6,21,10,23,19,12,4,
39 26,8,16,7,27,20,13,2,
40 41,52,31,37,47,55,30,40,
41 51,45,33,48,44,49,39,56,
42 34,53,46,42,50,36,29,32
43 };
44 //左移位数表
45 const static char loop_table[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
46 //S盒
47 const static char s_box[8][4][16]={
48 //s1
49 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
50 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
51 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
52 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
53 //s2
54 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
55 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
56 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
57 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
58 //s3
59 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
60 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
61 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
62 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
63 //s4
64 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
65 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
66 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
67 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
68 //s5
69 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
70 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
71 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
72 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
73 //s6
74 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
75 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
76 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
77 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
78 //s7
79 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
80 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
81 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
82 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
83 //s8
84 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
85 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
86 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
87 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
88 };
89 static bool subkey[16][48];//16圈子密钥
90 void des_run(char out[8],char in[8], bool type)
91 {
92 static bool m[64],tmp[32],*li=&m[0], *ri=&m[32];
93 bytetobit(m,in,64);
94 transform(m,m,ip_table,64);
95 if(type==encrypt){
96 for(int i=0;i<16;i++){
97 memcpy(tmp,ri,32);
98 f_func(ri,subkey[i]);
99 xor(ri,li,32);
100 memcpy(li,tmp,32);
101 }
102 }else{
103 for(int i=15;i>=0;i--){
104 memcpy(tmp,li,32);
105 f_func(li,subkey[i]);
106 xor(li,ri,32);
107 memcpy(ri,tmp,32);
108 }
109 }
110 transform(m,m,ipr_table,64);
111 bittobyte(out,m,64);
112 }
113 void des_setkey(const char key[8])
114 {
115 static bool k[64], *kl=&k[0], *kr=&k[28];
116 bytetobit(k,key,64);
117 transform(k,k,pc1_table,56);
118 for(int i=0;i<16;i++)
119 {
120 rotatel(kl,28,loop_table[i]);
121 rotatel(kr,28,loop_table[i]);
122 transform(subkey[i],k,pc2_table,48);
123 }
124 }
125 void f_func(bool in[32],const bool ki[48])
126 {
127 static bool mr[48];
128 transform(mr,in,e_table,48);
129 xor(mr,ki,48);
130 s_func(in,mr);
131 transform(in,in,p_table,32);
132 }
133 void s_func(bool out[32],const bool in[48])
134 {
135 for(char i=0,j,k;i<8;i++,in+=6,out+=4)
136 {
137 j=(in[0]<<1)+in[5];
138 k=(in[1]<<3)+(in[2]<<2)+(in[3]<<1)+in[4];
139 bytetobit(out,&s_box[i][j][k],4);
140 }
141 }
142 void transform(bool *out,bool *in,const char *table,int len)
143 {
144 static bool tmp[256];
145 for(int i=0;i<len;i++)
146 tmp[i]=in[table[i]-1];
147 memcpy(out,tmp,len);
148 }
149 void xor(bool *ina,const bool *inb,int len)
150 {
151 for(int i=0;i<len;i++)
152 ina[i]^=inb[i];
153 }
154 void rotatel(bool *in,int len,int loop)
155 {
156 static bool tmp[256];
157 memcpy(tmp,in,loop);
158 memcpy(in,in+loop,len-loop);
159 memcpy(in+len-loop,tmp,loop);
160 }
161 void bytetobit(bool *out,const char *in,int bits)
162 {
163 for(int i=0;i<bits;i++)
164 out[i]=(in[i/8]>>(i%8)) &1;
165 }
166 void bittobyte(char *out,const bool *in,int bits)
167 {
168 memset(out,0,(bits+7)/8);
169 for(int i=0;i<bits;i++)
170 out[i/8]|=in[i]<<(i%8);
171 }
172 void main()
173 {
174 char key[8]={'p','r','o','g','r','a','m'},str[8];
175 puts("*****************DES***********************");
176 printf("\n");
177 printf("\n");
178 puts("please input your words");
179 gets(str);
180 printf("\n");
181 puts("****************************************");
182 des_setkey(key);
183 des_run(str,str,encrypt);
184 puts("after encrypting:");
185 puts(str);
186 printf("\n");
187 puts("****************************************");
188 puts("after decrypting:");
189 des_run(str,str,decrypt);
190 puts(str);
191 printf("\n");
192 puts("****************************************");
193 printf("\n");
194 }