1、3des.cpp
[cpp] view plaincopy
1. /************************************************************************
2. * DES/3DES原理:
3. * 1-1、变换密钥
4. * 1-1-1、取得64位的密钥,每个第8位作为奇偶校验位。程序上无需任何操作
5. *
6. * 1-2、变换密钥
7. * 1-2-1、舍弃64位密钥中的奇偶校验位,根据下表(PC-1)进行密钥变换(换位)得到56位的密钥,在变换中,奇偶校验位被舍弃
8. *
9. * Permuted Choice 1 (PC-1),本表表示的数组下标从1开始,因此转换时需要:PC1_Table[i]-1
10. * 57, 49, 41, 33, 25, 17, 9,
11. * 1, 58, 50, 42, 34, 26, 18,
12. * 10, 2, 59, 51, 43, 35, 27,
13. * 19, 11, 3, 60, 52, 44, 36,
14. * 63, 55, 47, 39, 31, 23, 15,
15. * 7, 62, 54, 46, 38, 30, 22,
16. * 14, 6, 61, 53, 45, 37, 29,
17. * 21, 13, 5, 28, 20, 12, 4
18. *
19. * 1-2-2、将变换后的密钥分为两个部分,开始的28位称为C[0],最后的28位称为D[0]
20. * 1-2-3、生成16个子密钥, I = 1。<这里假设I为生成密钥的轮数,它的基数是从1开始,即I取值为 1 – 16 >
21. * 1-2-3-1、同时将C[I]、D[I]左移1位或2位,根据I值决定左移的位数。见下表:
22. * I: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
23. * 左移位数: 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
24. * <每轮移位输出的C[I] D[I]作为下一轮移位时输入的C[I] D[I]
25. * 这里有个关键点,进行解密时这里是向右移,而且在I = 1时无需移位。即:
26. * 右移位数: 0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
27. * 解密时也只有这里也有区别,其它的地方和加密是一样的,DES算法的很多换位表是互反的,即可能你用
28. * 表1换位完后,用表2再换一遍就又和原始值一样了>
29. * 1-2-3-2、将C[I]D[I]作为一个整体按下表(PC-2)变换,得到48位的K[I]
30. *
31. * Permuted Choice 2 (PC-2),本表表示的数组下标从1开始,因此转换时需要:PC2_Table[i]-1
32. * 14, 17, 11, 24, 1, 5,
33. * 3, 28, 15, 6, 21, 10,
34. * 23, 19, 12, 4, 26, 8,
35. * 16, 7, 27, 20, 13, 2,
36. * 41, 52, 31, 37, 47, 55,
37. * 30, 40, 51, 45, 33, 48,
38. * 44, 49, 39, 56, 34, 53,
39. * 46, 42, 50, 36, 29, 32
40. *
41. * 1-2-3-3、从1-2-3-1处循环执行,直到K[16]被计算完成
42. *
43. * 2、处理64位的数据
44. * 2-1、取得64位的数据,如果数据长度不足64位,应该将其扩展为64位(例如补零)
45. * <补什么要看具体数据,只能补数据取值范围外的字符,如果数据取值范围是0x00 – 0XFF而且还不是64位整倍数,那就要另想办法,比如在数据前面加上长度什么的>
46. * 2-2、将64位数据按下表变换(IP)
47. *
48. * Initial Permutation (IP),本表表示的数组下标从1开始,因此转换时需要:IP_Table[i]-1
49. * 58, 50, 42, 34, 26, 18, 10, 2,
50. * 60, 52, 44, 36, 28, 20, 12, 4,
51. * 62, 54, 46, 38, 30, 22, 14, 6,
52. * 64, 56, 48, 40, 32, 24, 16, 8,
53. * 57, 49, 41, 33, 25, 17, 9, 1,
54. * 59, 51, 43, 35, 27, 19, 11, 3,
55. * 61, 53, 45, 37, 29, 21, 13, 5,
56. * 63, 55, 47, 39, 31, 23, 15, 7
57. *
58. * 2-3、将变换后的数据分为两部分,开始的32位称为L[0],最后的32位称为R[0]
59. * 2-4、用16个子密钥加密数据,初始I=1
60. * 2-4-1、将32位的R[I-1]按下表(E)扩展为48位的E[I-1]
61. * Expansion (E),本表表示的数组下标从1开始,因此转换时需要:E_Table[i]-1
62. * 32, 1, 2, 3, 4, 5,
63. * 4, 5, 6, 7, 8, 9,
64. * 8, 9, 10, 11, 12, 13,
65. * 12, 13, 14, 15, 16, 17,
66. * 16, 17, 18, 19, 20, 21,
67. * 20, 21, 22, 23, 24, 25,
68. * 24, 25, 26, 27, 28, 29,
69. * 28, 29, 30, 31, 32, 1
70. *
71. * 2-4-2、异或E[I-1]和K[I],即E[I-1] XOR K[I] <上面生成的16组子密钥就是在这里用到的>
72. * 2-4-3、将异或后的结果分为8个6位长的部分,第1位到第6位称为B[1],第7位到第12位称为B[2],依此类推,第43位到第48位称为B[8]
73. * 2-4-4、按S表变换所有的B[J],初始J=1。所有在S表的值都被当作4位长度处理
74. * 2-4-4-1、将B[J]的第1位和第6位组合为一个2位长度的变量M,M作为在S[J]中的行号
75. * 2-4-4-2、将B[J]的第2位到第5位组合,作为一个4位长度的变量N,N作为在S[J]中的列号
76. * 2-4-4-3、用S[J][M][N]来取代B[J]
77. *
78. * Substitution Box
79. * S[1]
80. * 14 4 13 1 2 15 11 8 3 10 6 12 5 9 0 7
81. * 0 15 7 4 14 2 13 1 10 6 12 11 9 5 3 8
82. * 4 1 14 8 13 6 2 11 15 12 9 7 3 10 5 0
83. * 15 12 8 2 4 9 1 7 5 11 3 14 10 0 6 13
84. *
85. * S[2]
86. * 15 1 8 14 6 11 3 4 9 7 2 13 12 0 5 10
87. * 3 13 4 7 15 2 8 14 12 0 1 10 6 9 11 5
88. * 0 14 7 11 10 4 13 1 5 8 12 6 9 3 2 15
89. * 13 8 10 1 3 15 4 2 11 6 7 12 0 5 14 9
90. *
91. * S[3]
92. * 10 0 9 14 6 3 15 5 1 13 12 7 11 4 2 8
93. * 13 7 0 9 3 4 6 10 2 8 5 14 12 11 15 1
94. * 13 6 4 9 8 15 3 0 11 1 2 12 5 10 14 7
95. * 1 10 13 0 6 9 8 7 4 15 14 3 11 5 2 12
96. *
97. * S[4]
98. * 7 13 14 3 0 6 9 10 1 2 8 5 11 12 4 15
99. * 13 8 11 5 6 15 0 3 4 7 2 12 1 10 14 9
100. * 10 6 9 0 12 11 7 13 15 1 3 14 5 2 8 4
101. * 3 15 0 6 10 1 13 8 9 4 5 11 12 7 2 14
102. *
103. * S[5]
104. * 2 12 4 1 7 10 11 6 8 5 3 15 13 0 14 9
105. * 14 11 2 12 4 7 13 1 5 0 15 10 3 9 8 6
106. * 4 2 1 11 10 13 7 8 15 9 12 5 6 3 0 14
107. * 11 8 12 7 1 14 2 13 6 15 0 9 10 4 5 3
108. *
109. * S[6]
110. * 12 1 10 15 9 2 6 8 0 13 3 4 14 7 5 11
111. * 10 15 4 2 7 12 9 5 6 1 13 14 0 11 3 8
112. * 9 14 15 5 2 8 12 3 7 0 4 10 1 13 11 6
113. * 4 3 2 12 9 5 15 10 11 14 1 7 6 0 8 13
114. *
115. * S[7]
116. * 4 11 2 14 15 0 8 13 3 12 9 7 5 10 6 1
117. * 13 0 11 7 4 9 1 10 14 3 5 12 2 15 8 6
118. * 1 4 11 13 12 3 7 14 10 15 6 8 0 5 9 2
119. * 6 11 13 8 1 4 10 7 9 5 0 15 14 2 3 12
120. *
121. * S[8]
122. * 13 2 8 4 6 15 11 1 10 9 3 14 5 0 12 7
123. * 1 15 13 8 10 3 7 4 12 5 6 11 0 14 9 2
124. * 7 11 4 1 9 12 14 2 0 6 10 13 15 3 5 8
125. * 2 1 14 7 4 10 8 13 15 12 9 0 3 5 6 11
126. *
127. * 2-4-4-4、从2-4-4-1处循环执行,直到B[8]被替代完成
128. * 2-4-4-5、将B[1]到B[8]组合,按下表(P)变换,得到P <B[1]到B[8]组成一个32位的数据>
129. *
130. * Permutation P
131. * 16, 7, 20, 21,
132. * 29, 12, 28, 17,
133. * 1, 15, 23, 26,
134. * 5, 18, 31, 10,
135. * 2, 8, 24, 14,
136. * 32, 27, 3, 9,
137. * 19, 13, 30, 6,
138. * 22, 11, 4, 25
139. *
140. * 2-4-6、异或P和L[I-1]结果放在R[I],即R[I] = P XOR L[I - 1]
141. * 2-4-7、L[I] = R[I - 1]
142. * 2-4-8、从2-4-1处开始循环执行,直到K[16]被变换完成
143. * 2-4-5、组合变换后的R[16]L[16](注意:R作为开始的32位),按下表(IP-1)变换得到最后的结果
144. * <这里要注意了,是对R[16]L[16],而不是L[16]R[16] 即,需要进行左32位和右32位调换一下位置, 这里下标里的16是指轮数,不是位数>
145. *
146. * Final Permutation (IP**-1)
147. * 40, 8, 48, 16, 56, 24, 64, 32,
148. * 39, 7, 47, 15, 55, 23, 63, 31,
149. * 38, 6, 46, 14, 54, 22, 62, 30,
150. * 37, 5, 45, 13, 53, 21, 61, 29,
151. * 36, 4, 44, 12, 52, 20, 60, 28,
152. * 35, 3, 43, 11, 51, 19, 59, 27,
153. * 34, 2, 42, 10, 50, 18, 58, 26,
154. * 33, 1, 41, 9, 49, 17, 57, 25
155. *
156. * 3DES计算方法如下:
157. * 3DES(加密) = DES(key1, 加密) DES(key2, 解密) DES(key3, 加密)
158. * 3DES(解密) = DES(key3, 解密) DES(key2, 加密) DES(key1, 解密)
159. * 每个KEY为64位,总共可以有192位的KEY, 但一般都只使用128位的key
160. * 如果只用128位密钥,则key3 = key1
161. *
162. *************************************************************************/
163.
164. #include "3des.h"
165. #include <stdlib.h>
166. #include <stdio.h>
167. #include <memory.h>
168. #include <string.h>
169. #include <ctype.h>
170.
171. const char IP_Table[64] =
172. {
173. 58, 50, 42, 34, 26, 18, 10, 2,
174. 60, 52, 44, 36, 28, 20, 12, 4,
175. 62, 54, 46, 38, 30, 22, 14, 6,
176. 64, 56, 48, 40, 32, 24, 16, 8,
177. 57, 49, 41, 33, 25, 17, 9, 1,
178. 59, 51, 43, 35, 27, 19, 11, 3,
179. 61, 53, 45, 37, 29, 21, 13, 5,
180. 63, 55, 47, 39, 31, 23, 15, 7
181. };
182.
183. const char IPR_Table[64] =
184. {
185. 40, 8, 48, 16, 56, 24, 64, 32,
186. 39, 7, 47, 15, 55, 23, 63, 31,
187. 38, 6, 46, 14, 54, 22, 62, 30,
188. 37, 5, 45, 13, 53, 21, 61, 29,
189. 36, 4, 44, 12, 52, 20, 60, 28,
190. 35, 3, 43, 11, 51, 19, 59, 27,
191. 34, 2, 42, 10, 50, 18, 58, 26,
192. 33, 1, 41, 9, 49, 17, 57, 25
193. };
194.
195. const char E_Table[48] =
196. {
197. 32, 1, 2, 3, 4, 5,
198. 4, 5, 6, 7, 8, 9,
199. 8, 9, 10, 11, 12, 13,
200. 12, 13, 14, 15, 16, 17,
201. 16, 17, 18, 19, 20, 21,
202. 20, 21, 22, 23, 24, 25,
203. 24, 25, 26, 27, 28, 29,
204. 28, 29, 30, 31, 32, 1
205. };
206.
207. const char P_Table[32] =
208. {
209. 16, 7, 20, 21,
210. 29, 12, 28, 17,
211. 1, 15, 23, 26,
212. 5, 18, 31, 10,
213. 2, 8, 24, 14,
214. 32, 27, 3, 9,
215. 19, 13, 30, 6,
216. 22, 11, 4, 25
217. };
218.
219. const char PC1_Table[56] =
220. {
221. 57, 49, 41, 33, 25, 17, 9,
222. 1, 58, 50, 42, 34, 26, 18,
223. 10, 2, 59, 51, 43, 35, 27,
224. 19, 11, 3, 60, 52, 44, 36,
225. 63, 55, 47, 39, 31, 23, 15,
226. 7, 62, 54, 46, 38, 30, 22,
227. 14, 6, 61, 53, 45, 37, 29,
228. 21, 13, 5, 28, 20, 12, 4
229. };
230.
231. const char PC2_Table[48] =
232. {
233. 14, 17, 11, 24, 1, 5,
234. 3, 28, 15, 6, 21, 10,
235. 23, 19, 12, 4, 26, 8,
236. 16, 7, 27, 20, 13, 2,
237. 41, 52, 31, 37, 47, 55,
238. 30, 40, 51, 45, 33, 48,
239. 44, 49, 39, 56, 34, 53,
240. 46, 42, 50, 36, 29, 32
241. };
242.
243. const char LOOP_Table[16] =
244. {
245. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
246. };
247.
248. const char S_Box[8][4][16] =
249. {
250. {
251. {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
252. { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
253. { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
254. {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
255. },
256.
257. {
258. {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
259. { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
260. { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
261. {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}
262. },
263.
264. {
265. {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
266. {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
267. {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
268. { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}
269. },
270.
271. {
272. { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
273. {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
274. {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
275. { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}
276. },
277.
278. {
279. { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
280. {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
281. { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
282. {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}