码迷,mamicode.com
首页 > 编程语言 > 详细

DES加解密算法的简单实现

时间:2016-04-17 00:20:46      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

前几天刚写完一个简单的DES算法的实验,拿来作为第一次发到博客的随笔,填充一下空空如也的博客,献丑了

因为主要目的是Easy-To-Understand,再现一个直观的DES加解密的过程,所以很浪费地每一个数据位都用一个short整型存储,用来理ying解fu过zuo程ye就好(虽说DES这种对称加密算法十多年前就已经被淘汰了,现在一般建议用AES或者DES3

“1973 年,美国国家标准局(NBS)开始征集一种标准的数据加密标准算法(DES),以用于非机密性政府机构、商业部门和民间的对非机密的敏感数据进行加密。IBM 公司在1971年完成的LUCIFER 密码(64 比特分组,128 比特密钥)的基础上,改进成为建议的DES。改进后的DES算法仅使用56比特密钥,同时对S盒的修改被列入官方机密,曾广受批评。1975 年3 月17 日,NBS 公布了这个算法,并说明要以它作为联邦信息处理标准,征求各方意见。1977 年1 月15 日,建议被批准为联邦标准—FIPSPUB 46,并设计推出了DES 芯片。1981 年,ANSI 将DES 作为标准,即DEA[ANSI X3.92]。1983 年,ISO 采用DES 作为标准,即DEA-1。DES(Data Encryption Standard)是一个优秀的对称分组密码算法,直到2000 年10 月2 日NIST 宣布AES 算法前,其一直是业界的标准。”

 

技术分享
1 #pragma once
2 
3 void des_setup(const short* key);
4 void des_encrypt(const short *pt, short *ct);
5 void des_decrypt(const short *pt, short *ct);
DES.h
技术分享
  1 short sKey[16][48];
  2 
  3 short sBox_1[4][16] = {
  4     14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  5      0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  6      4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  7     15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13
  8 };
  9 
 10 short sBox_2[4][16] = {
 11     15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
 12      3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
 13      0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
 14     13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9
 15 };
 16 
 17 short sBox_3[4][16] = {
 18     10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
 19     13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
 20     13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
 21      1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12
 22 };
 23 
 24 short sBox_4[4][16] = {
 25      7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
 26     13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
 27     10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
 28      3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14
 29 };
 30 
 31 short sBox_5[4][16] = {
 32      2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
 33     14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
 34      4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
 35     11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3
 36 };
 37 
 38 short sBox_6[4][16] = {
 39     12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
 40     10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
 41      9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
 42      4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13
 43 };
 44 
 45 short sBox_7[4][16] = {
 46      4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
 47     13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
 48      1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
 49      6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12
 50 };
 51 
 52 short sBox_8[4][16] = {
 53     13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
 54      1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
 55      7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
 56      2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
 57 };
 58 
 59 short KuoChongZhiHuan_E[48] = {
 60     32,  1,  2,  3,  4,  5,
 61      4,  5,  6,  7,  8,  9,
 62      8,  9, 10, 11, 12, 13,
 63     12, 13, 14, 15, 16, 17,
 64     16, 17, 18, 19, 20, 21,
 65     20, 21, 22, 23, 24, 25,
 66     24, 25, 26, 27, 28, 29,
 67     28, 29, 30, 31, 32,  1
 68 };
 69 
 70 short ZhiHuanHanShu_P[32] = {
 71     16,  7, 20, 21, 29, 12, 28, 17,
 72      1, 15, 23, 26,  5, 18, 31, 10,
 73      2,  8, 24, 14, 32, 27,  3,  9,
 74     19, 13, 30,  6, 22, 11,  4, 25
 75 };
 76 
 77 short ZhiHuanXuanZe_1[56] = {
 78     57, 49, 41, 33, 25, 17,  9,
 79      1, 58, 50, 42, 34, 26, 18,
 80     10,  2, 59, 51, 43, 35, 27,
 81     19, 11,  3, 60, 52, 44, 36,
 82     63, 55, 47, 39, 31, 23, 15,
 83      7, 62, 54, 46, 38, 30, 22,
 84     14,  6, 61, 53, 45, 37, 29,
 85     21, 13,  5, 28, 20, 12,  4
 86 };
 87 
 88 short XunHuanZuoYi[16] = {
 89     1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
 90 };
 91 
 92 short ZhiHuanXuanZe_2[48] = {
 93     14, 17, 11, 24,  1,  5,  3, 28,
 94     15,  6, 21, 10, 23, 19, 12,  4,
 95     26,  8, 16,  7, 27, 20, 13,  2,
 96     41, 52, 31, 37, 47, 55, 30, 40,
 97     51, 45, 33, 48, 44, 49, 39, 56,
 98     34, 53, 46, 42, 50, 36, 29, 32
 99 };
100 
101 short ChuShiZhiHuan[64] = {
102     58, 50, 42, 34, 26, 18, 10,  2,
103     60, 52, 44, 36, 28, 20, 12,  4,
104     62, 54, 46, 38, 30, 22, 14,  6,
105     64, 56, 48, 40, 32, 24, 16,  8,
106     57, 49, 41, 33, 25, 17,  9,  1,
107     59, 51, 43, 35, 27, 19, 11,  3,
108     61, 53, 45, 37, 29, 21, 13,  5,
109     63, 55, 47, 39, 31, 23, 15,  7
110 };
111 
112 short NiChuShiZhiHuan[64] = {
113     40,  8, 48, 16, 56, 24, 64, 32,
114     39,  7, 47, 15, 55, 23, 63, 31,
115     38,  6, 46, 14, 54, 22, 62, 30,
116     37,  5, 45, 13, 53, 21, 61, 29,
117     36,  4, 44, 12, 52, 20, 60, 28,
118     35,  3, 43, 11, 51, 19, 59, 27,
119     34,  2, 42, 10, 50, 18, 58, 26,
120     33,  1, 41,  9, 49, 17, 57, 25
121 };
122 
123 void des_setup(const short* key)
124 {
125     short tempKey[56];
126     for (int i = 0; i < 56; ++i) {
127         tempKey[i] = key[ZhiHuanXuanZe_1[i] - 1];
128     }
129     for (int i = 0; i < 16; ++i) {
130         for (int j = 0; j < XunHuanZuoYi[i]; ++j) {
131             short temp = tempKey[0];
132             for (int m = 0; m < 27; ++m) {
133                 tempKey[m] = tempKey[m + 1];
134             }
135             tempKey[27] = temp;
136             temp = tempKey[28];
137             for (int m = 28; m < 55; ++m) {
138                 tempKey[m] = tempKey[m + 1];
139             }
140             tempKey[55] = temp;
141         }
142         for (int j = 0; j < 48; ++j) {
143             sKey[i][j] = tempKey[ZhiHuanXuanZe_2[j] - 1];
144         }
145     }
146 }
147 
148 void desfun(short *BlockL, short *BlockR, int round)
149 {
150     short tempBlock_48[48];
151     //扩充E
152     for (int i = 0; i < 48; ++i) {
153         tempBlock_48[i] = *(BlockR + KuoChongZhiHuan_E[i] - 1);
154     }
155     //异或
156     for (int i = 0; i < 48; ++i) {
157         if (tempBlock_48[i] == sKey[round][i])
158             tempBlock_48[i] = 0;
159         else
160             tempBlock_48[i] = 1;
161     }
162     //S盒代换
163     short tempBlock_32[32];
164     for (int i = 0, j = 0; i < 48; i += 6, j += 4) {
165         switch (sBox_1[2 * tempBlock_48[i] + tempBlock_48[5]][8 * tempBlock_48[i + 1] + 4 * tempBlock_48[i + 2] + 2 * tempBlock_48[i + 3] + tempBlock_48[i + 4]]) {
166         case 0:
167             tempBlock_32[j] = 0;
168             tempBlock_32[j + 1] = 0;
169             tempBlock_32[j + 2] = 0;
170             tempBlock_32[j + 3] = 0;
171             break;
172         case 1:
173             tempBlock_32[j] = 0;
174             tempBlock_32[j + 1] = 0;
175             tempBlock_32[j + 2] = 0;
176             tempBlock_32[j + 3] = 1;
177             break;
178         case 2:
179             tempBlock_32[j] = 0;
180             tempBlock_32[j + 1] = 0;
181             tempBlock_32[j + 2] = 1;
182             tempBlock_32[j + 3] = 0;
183             break;
184         case 3:
185             tempBlock_32[j] = 0;
186             tempBlock_32[j + 1] = 0;
187             tempBlock_32[j + 2] = 1;
188             tempBlock_32[j + 3] = 1;
189             break;
190         case 4:
191             tempBlock_32[j] = 0;
192             tempBlock_32[j + 1] = 1;
193             tempBlock_32[j + 2] = 0;
194             tempBlock_32[j + 3] = 0;
195             break;
196         case 5:
197             tempBlock_32[j] = 0;
198             tempBlock_32[j + 1] = 1;
199             tempBlock_32[j + 2] = 0;
200             tempBlock_32[j + 3] = 1;
201             break;
202         case 6:
203             tempBlock_32[j] = 0;
204             tempBlock_32[j + 1] = 1;
205             tempBlock_32[j + 2] = 1;
206             tempBlock_32[j + 3] = 0;
207             break;
208         case 7:
209             tempBlock_32[j] = 0;
210             tempBlock_32[j + 1] = 1;
211             tempBlock_32[j + 2] = 1;
212             tempBlock_32[j + 3] = 1;
213             break;
214         case 8:
215             tempBlock_32[j] = 1;
216             tempBlock_32[j + 1] = 0;
217             tempBlock_32[j + 2] = 0;
218             tempBlock_32[j + 3] = 0;
219             break;
220         case 9:
221             tempBlock_32[j] = 1;
222             tempBlock_32[j + 1] = 0;
223             tempBlock_32[j + 2] = 0;
224             tempBlock_32[j + 3] = 1;
225             break;
226         case 10:
227             tempBlock_32[j] = 1;
228             tempBlock_32[j + 1] = 0;
229             tempBlock_32[j + 2] = 1;
230             tempBlock_32[j + 3] = 0;
231             break;
232         case 11:
233             tempBlock_32[j] = 1;
234             tempBlock_32[j + 1] = 0;
235             tempBlock_32[j + 2] = 1;
236             tempBlock_32[j + 3] = 1;
237             break;
238         case 12:
239             tempBlock_32[j] = 1;
240             tempBlock_32[j + 1] = 1;
241             tempBlock_32[j + 2] = 0;
242             tempBlock_32[j + 3] = 0;
243             break;
244         case 13:
245             tempBlock_32[j] = 1;
246             tempBlock_32[j + 1] = 1;
247             tempBlock_32[j + 2] = 0;
248             tempBlock_32[j + 3] = 1;
249             break;
250         case 14:
251             tempBlock_32[j] = 1;
252             tempBlock_32[j + 1] = 1;
253             tempBlock_32[j + 2] = 1;
254             tempBlock_32[j + 3] = 0;
255             break;
256         case 15:
257             tempBlock_32[j] = 1;
258             tempBlock_32[j + 1] = 1;
259             tempBlock_32[j + 2] = 1;
260             tempBlock_32[j + 3] = 1;
261             break;
262         }
263     }
264     //置换P
265     short afterP[32];
266     for (int i = 0; i < 32; ++i) {
267         afterP[i] = tempBlock_32[ZhiHuanHanShu_P[i] - 1];
268     }
269     //备份Ri-1
270     short oldBlockR[32];
271     for (int i = 0; i < 32; ++i) {
272         oldBlockR[i] = BlockR[i];
273     }
274     //异或=>Ri
275     for (int i = 0; i < 32; ++i) {
276         if (afterP[i] == *(BlockL + i))
277             BlockR[i] = 0;
278         else
279             BlockR[i] = 1;
280     }
281     //Ri-1=>Li
282     for (int i = 0; i < 32; ++i) {
283         BlockL[i] = oldBlockR[i];
284     }
285 }
286 
287 void des_encrypt(const short *pt, short *ct)
288 {
289     short Block[64];
290     short BlockL[32];
291     short BlockR[32];
292     //初始置换
293     for (int i = 0; i < 64; ++i) {
294         Block[i] = pt[ChuShiZhiHuan[i] - 1];
295     }
296     //分成左右两块
297     for (int i = 0; i < 32; ++i) {
298         BlockL[i] = Block[i];
299         BlockR[i] = Block[i + 32];
300     }
301     //16轮desfun
302     for (int i = 0; i < 16; ++i) {
303         desfun(BlockL, BlockR, i);
304     }
305     //32位互换
306     for (int i = 0; i < 32; ++i) {
307         short temp = BlockL[i];
308         BlockL[i] = BlockR[i];
309         BlockR[i] = temp;
310     }
311     for (int i = 0; i < 32; ++i) {
312         Block[i] = BlockL[i];
313         Block[i + 32] = BlockR[i];
314     }
315     //逆初始置换
316     for (int i = 0; i < 64; ++i) {
317         ct[i] = Block[NiChuShiZhiHuan[i] - 1];
318     }
319 }
320 
321 void des_decrypt(const short *pt, short *ct)
322 {
323     short Block[64];
324     short BlockL[32];
325     short BlockR[32];
326     //初始置换
327     for (int i = 0; i < 64; ++i) {
328         Block[i] = pt[ChuShiZhiHuan[i] - 1];
329     }
330     //分成左右两块
331     for (int i = 0; i < 32; ++i) {
332         BlockL[i] = Block[i];
333         BlockR[i] = Block[i + 32];
334     }
335     //16轮desfun(与加密唯一的不同处)
336     for (int i = 15; i >= 0; --i) {
337         desfun(BlockL, BlockR, i);
338     }
339     //32位互换
340     for (int i = 0; i < 32; ++i) {
341         short temp = BlockL[i];
342         BlockL[i] = BlockR[i];
343         BlockR[i] = temp;
344     }
345     for (int i = 0; i < 32; ++i) {
346         Block[i] = BlockL[i];
347         Block[i + 32] = BlockR[i];
348     }
349     //逆初始置换
350     for (int i = 0; i < 64; ++i) {
351         ct[i] = Block[NiChuShiZhiHuan[i] - 1];
352     }
353 }
DES.cpp

 

DES加解密算法的简单实现

标签:

原文地址:http://www.cnblogs.com/shing0315/p/5399724.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!