WNDES.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. //**************************************************************************
  2. //*Des加密算法
  3. //**************************************************************************
  4. unit WNDES;
  5. interface
  6. uses
  7. Windows, Messages, ShlObj, WinSock, IniFiles, ActiveX, SysUtils,
  8. Classes, Graphics, Controls, Forms, Dialogs;
  9. type
  10. TKeyByte = array[0..5] of Byte;
  11. TDesMode = (dmEncry, dmDESry);
  12. const
  13. BitIP: array[0..63] of Byte =
  14. (57, 49, 41, 33, 25, 17, 9, 1,
  15. 59, 51, 43, 35, 27, 19, 11, 3,
  16. 61, 53, 45, 37, 29, 21, 13, 5,
  17. 63, 55, 47, 39, 31, 23, 15, 7,
  18. 56, 48, 40, 32, 24, 16, 8, 0,
  19. 58, 50, 42, 34, 26, 18, 10, 2,
  20. 60, 52, 44, 36, 28, 20, 12, 4,
  21. 62, 54, 46, 38, 30, 22, 14, 6);
  22. BitCP: array[0..63] of Byte =
  23. (39, 7, 47, 15, 55, 23, 63, 31,
  24. 38, 6, 46, 14, 54, 22, 62, 30,
  25. 37, 5, 45, 13, 53, 21, 61, 29,
  26. 36, 4, 44, 12, 52, 20, 60, 28,
  27. 35, 3, 43, 11, 51, 19, 59, 27,
  28. 34, 2, 42, 10, 50, 18, 58, 26,
  29. 33, 1, 41, 9, 49, 17, 57, 25,
  30. 32, 0, 40, 8, 48, 16, 56, 24);
  31. BitExp: array[0..47] of Integer =
  32. (31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9, 10,
  33. 11, 12, 11, 12, 13, 14, 15, 16, 15, 16, 17, 18, 19, 20, 19, 20,
  34. 21, 22, 23, 24, 23, 24, 25, 26, 27, 28, 27, 28, 29, 30, 31, 0);
  35. BitPM: array[0..31] of Byte =
  36. (15, 6, 19, 20, 28, 11, 27, 16, 0, 14, 22, 25, 4, 17, 30, 9,
  37. 1, 7, 23, 13, 31, 26, 2, 8, 18, 12, 29, 5, 21, 10, 3, 24);
  38. sBox: array[0..7] of array[0..63] of Byte =
  39. ((14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  40. 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  41. 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  42. 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13),
  43. (15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  44. 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  45. 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  46. 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9),
  47. (10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  48. 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  49. 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  50. 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12),
  51. (7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  52. 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  53. 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  54. 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14),
  55. (2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  56. 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  57. 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  58. 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3),
  59. (12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  60. 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  61. 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  62. 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13),
  63. (4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  64. 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  65. 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  66. 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12),
  67. (13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  68. 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  69. 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  70. 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11));
  71. BitPMC1: array[0..55] of Byte =
  72. (56, 48, 40, 32, 24, 16, 8,
  73. 0, 57, 49, 41, 33, 25, 17,
  74. 9, 1, 58, 50, 42, 34, 26,
  75. 18, 10, 2, 59, 51, 43, 35,
  76. 62, 54, 46, 38, 30, 22, 14,
  77. 6, 61, 53, 45, 37, 29, 21,
  78. 13, 5, 60, 52, 44, 36, 28,
  79. 20, 12, 4, 27, 19, 11, 3);
  80. BitPMC2: array[0..47] of Byte =
  81. (13, 16, 10, 23, 0, 4,
  82. 2, 27, 14, 5, 20, 9,
  83. 22, 18, 11, 3, 25, 7,
  84. 15, 6, 26, 19, 12, 1,
  85. 40, 51, 30, 36, 46, 54,
  86. 29, 39, 50, 44, 32, 47,
  87. 43, 48, 38, 55, 33, 52,
  88. 45, 41, 49, 35, 28, 31);
  89. //////////////////DES加密算法////////////////////////////
  90. function EncryStr(Str, Key: string): string; //加密
  91. function EncryStrHex(Str, Key: string): string; //以十六进制加密(有些时侯加密的密文含有特殊字符文本框不能显示、数据库文本字段也不能存储。以十六进制形式表示可保证绝对不会出现该种情况)
  92. function DESryStr(Str, Key: string): string; //解密
  93. function DESryStrHex(StrHex, Key: string): string; //解密十六进制加密的密文
  94. var
  95. subKey: array[0..15] of TKeyByte;
  96. implementation
  97. procedure initPermutation(var inData: array of Byte);
  98. var
  99. newData: array[0..7] of Byte;
  100. i: Integer;
  101. begin
  102. FillChar(newData, 8, 0);
  103. for i := 0 to 63 do
  104. if (inData[BitIP[i] shr 3] and (1 shl (7 - (BitIP[i] and $07)))) <> 0 then
  105. newData[i shr 3] := newData[i shr 3] or (1 shl (7 - (i and $07)));
  106. for i := 0 to 7 do inData[i] := newData[i];
  107. end;
  108. procedure conversePermutation(var inData: array of Byte);
  109. var
  110. newData: array[0..7] of Byte;
  111. i: Integer;
  112. begin
  113. FillChar(newData, 8, 0);
  114. for i := 0 to 63 do
  115. if (inData[BitCP[i] shr 3] and (1 shl (7 - (BitCP[i] and $07)))) <> 0 then
  116. newData[i shr 3] := newData[i shr 3] or (1 shl (7 - (i and $07)));
  117. for i := 0 to 7 do inData[i] := newData[i];
  118. end;
  119. procedure expand(inData: array of Byte; var outData: array of Byte);
  120. var
  121. i: Integer;
  122. begin
  123. FillChar(outData, 6, 0);
  124. for i := 0 to 47 do
  125. if (inData[BitExp[i] shr 3] and (1 shl (7 - (BitExp[i] and $07)))) <> 0 then
  126. outData[i shr 3] := outData[i shr 3] or (1 shl (7 - (i and $07)));
  127. end;
  128. procedure permutation(var inData: array of Byte);
  129. var
  130. newData: array[0..3] of Byte;
  131. i: Integer;
  132. begin
  133. FillChar(newData, 4, 0);
  134. for i := 0 to 31 do
  135. if (inData[BitPM[i] shr 3] and (1 shl (7 - (BitPM[i] and $07)))) <> 0 then
  136. newData[i shr 3] := newData[i shr 3] or (1 shl (7 - (i and $07)));
  137. for i := 0 to 3 do inData[i] := newData[i];
  138. end;
  139. function si(s, inByte: Byte): Byte;
  140. var
  141. c: Byte;
  142. begin
  143. c := (inByte and $20) or ((inByte and $1E) shr 1) or
  144. ((inByte and $01) shl 4);
  145. Result := (sBox[s][c] and $0F);
  146. end;
  147. procedure permutationChoose1(inData: array of Byte;
  148. var outData: array of Byte);
  149. var
  150. i: Integer;
  151. begin
  152. FillChar(outData, 7, 0);
  153. for i := 0 to 55 do
  154. if (inData[BitPMC1[i] shr 3] and (1 shl (7 - (BitPMC1[i] and $07)))) <> 0 then
  155. outData[i shr 3] := outData[i shr 3] or (1 shl (7 - (i and $07)));
  156. end;
  157. procedure permutationChoose2(inData: array of Byte;
  158. var outData: array of Byte);
  159. var
  160. i: Integer;
  161. begin
  162. FillChar(outData, 6, 0);
  163. for i := 0 to 47 do
  164. if (inData[BitPMC2[i] shr 3] and (1 shl (7 - (BitPMC2[i] and $07)))) <> 0 then
  165. outData[i shr 3] := outData[i shr 3] or (1 shl (7 - (i and $07)));
  166. end;
  167. procedure cycleMove(var inData: array of Byte; bitMove: Byte);
  168. var
  169. i: Integer;
  170. begin
  171. for i := 0 to bitMove - 1 do
  172. begin
  173. inData[0] := (inData[0] shl 1) or (inData[1] shr 7);
  174. inData[1] := (inData[1] shl 1) or (inData[2] shr 7);
  175. inData[2] := (inData[2] shl 1) or (inData[3] shr 7);
  176. inData[3] := (inData[3] shl 1) or ((inData[0] and $10) shr 4);
  177. inData[0] := (inData[0] and $0F);
  178. end;
  179. end;
  180. procedure makeKey(inKey: array of Byte; var outKey: array of TKeyByte);
  181. const
  182. bitDisplace: array[0..15] of Byte =
  183. (1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1);
  184. var
  185. outData56: array[0..6] of Byte;
  186. key28l: array[0..3] of Byte;
  187. key28r: array[0..3] of Byte;
  188. key56o: array[0..6] of Byte;
  189. i: Integer;
  190. begin
  191. permutationChoose1(inKey, outData56);
  192. key28l[0] := outData56[0] shr 4;
  193. key28l[1] := (outData56[0] shl 4) or (outData56[1] shr 4);
  194. key28l[2] := (outData56[1] shl 4) or (outData56[2] shr 4);
  195. key28l[3] := (outData56[2] shl 4) or (outData56[3] shr 4);
  196. key28r[0] := outData56[3] and $0F;
  197. key28r[1] := outData56[4];
  198. key28r[2] := outData56[5];
  199. key28r[3] := outData56[6];
  200. for i := 0 to 15 do
  201. begin
  202. cycleMove(key28l, bitDisplace[i]);
  203. cycleMove(key28r, bitDisplace[i]);
  204. key56o[0] := (key28l[0] shl 4) or (key28l[1] shr 4);
  205. key56o[1] := (key28l[1] shl 4) or (key28l[2] shr 4);
  206. key56o[2] := (key28l[2] shl 4) or (key28l[3] shr 4);
  207. key56o[3] := (key28l[3] shl 4) or (key28r[0]);
  208. key56o[4] := key28r[1];
  209. key56o[5] := key28r[2];
  210. key56o[6] := key28r[3];
  211. permutationChoose2(key56o, outKey[i]);
  212. end;
  213. end;
  214. procedure encry(inData, subKey: array of Byte;
  215. var outData: array of Byte);
  216. var
  217. outBuf: array[0..5] of Byte;
  218. buf: array[0..7] of Byte;
  219. i: Integer;
  220. begin
  221. expand(inData, outBuf);
  222. for i := 0 to 5 do outBuf[i] := outBuf[i] xor subKey[i];
  223. // outBuf xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
  224. buf[0] := outBuf[0] shr 2; //xxxxxx -> 2
  225. buf[1] := ((outBuf[0] and $03) shl 4) or (outBuf[1] shr 4); // 4 <- xx xxxx -> 4
  226. buf[2] := ((outBuf[1] and $0F) shl 2) or (outBuf[2] shr 6); // 2 <- xxxx xx -> 6
  227. buf[3] := outBuf[2] and $3F; // xxxxxx
  228. buf[4] := outBuf[3] shr 2; // xxxxxx
  229. buf[5] := ((outBuf[3] and $03) shl 4) or (outBuf[4] shr 4); // xx xxxx
  230. buf[6] := ((outBuf[4] and $0F) shl 2) or (outBuf[5] shr 6); // xxxx xx
  231. buf[7] := outBuf[5] and $3F; // xxxxxx
  232. for i := 0 to 7 do buf[i] := si(i, buf[i]);
  233. for i := 0 to 3 do outBuf[i] := (buf[i * 2] shl 4) or buf[i * 2 + 1];
  234. permutation(outBuf);
  235. for i := 0 to 3 do outData[i] := outBuf[i];
  236. end;
  237. procedure desData(desMode: TDesMode;
  238. inData: array of Byte; var outData: array of Byte);
  239. // inData, outData 都为8Bytes,否则出错
  240. var
  241. i, j: Integer;
  242. temp, buf: array[0..3] of Byte;
  243. begin
  244. for i := 0 to 7 do outData[i] := inData[i];
  245. initPermutation(outData);
  246. if desMode = dmEncry then
  247. begin
  248. for i := 0 to 15 do
  249. begin
  250. for j := 0 to 3 do temp[j] := outData[j]; //temp = Ln
  251. for j := 0 to 3 do outData[j] := outData[j + 4]; //Ln+1 = Rn
  252. encry(outData, subKey[i], buf); //Rn ==Kn==> buf
  253. for j := 0 to 3 do outData[j + 4] := temp[j] xor buf[j]; //Rn+1 = Ln^buf
  254. end;
  255. for j := 0 to 3 do temp[j] := outData[j + 4];
  256. for j := 0 to 3 do outData[j + 4] := outData[j];
  257. for j := 0 to 3 do outData[j] := temp[j];
  258. end
  259. else if desMode = dmDESry then
  260. begin
  261. for i := 15 downto 0 do
  262. begin
  263. for j := 0 to 3 do temp[j] := outData[j];
  264. for j := 0 to 3 do outData[j] := outData[j + 4];
  265. encry(outData, subKey[i], buf);
  266. for j := 0 to 3 do outData[j + 4] := temp[j] xor buf[j];
  267. end;
  268. for j := 0 to 3 do temp[j] := outData[j + 4];
  269. for j := 0 to 3 do outData[j + 4] := outData[j];
  270. for j := 0 to 3 do outData[j] := temp[j];
  271. end;
  272. conversePermutation(outData);
  273. end;
  274. function EncryStr(Str, Key: string): string;
  275. var
  276. StrByte, OutByte, KeyByte: array[0..7] of Byte;
  277. StrResult: string;
  278. I, J: Integer;
  279. begin
  280. if (Length(Str) > 0) and (Ord(Str[Length(Str)]) = 0) then
  281. raise Exception.Create('Error: the last char is NULL char.');
  282. if Length(Key) < 8 then
  283. while Length(Key) < 8 do Key := Key + Chr(0);
  284. while Length(Str) mod 8 <> 0 do Str := Str + Chr(0);
  285. for J := 0 to 7 do KeyByte[J] := Ord(Key[J + 1]);
  286. makeKey(keyByte, subKey);
  287. StrResult := '';
  288. for I := 0 to Length(Str) div 8 - 1 do
  289. begin
  290. for J := 0 to 7 do
  291. StrByte[J] := Ord(Str[I * 8 + J + 1]);
  292. desData(dmEncry, StrByte, OutByte);
  293. for J := 0 to 7 do
  294. StrResult := StrResult + Chr(OutByte[J]);
  295. end;
  296. Result := StrResult;
  297. end;
  298. function DESryStr(Str, Key: string): string;
  299. var
  300. StrByte, OutByte, KeyByte: array[0..7] of Byte;
  301. StrResult: string;
  302. I, J: Integer;
  303. begin
  304. if Length(Key) < 8 then
  305. while Length(Key) < 8 do Key := Key + Chr(0);
  306. for J := 0 to 7 do KeyByte[J] := Ord(Key[J + 1]);
  307. makeKey(keyByte, subKey);
  308. StrResult := '';
  309. for I := 0 to Length(Str) div 8 - 1 do
  310. begin
  311. for J := 0 to 7 do StrByte[J] := Ord(Str[I * 8 + J + 1]);
  312. desData(dmDESry, StrByte, OutByte);
  313. for J := 0 to 7 do
  314. StrResult := StrResult + Chr(OutByte[J]);
  315. end;
  316. while (Length(StrResult) > 0) and
  317. (Ord(StrResult[Length(StrResult)]) = 0) do
  318. Delete(StrResult, Length(StrResult), 1);
  319. Result := StrResult;
  320. end;
  321. ///////////////////////////////////////////////////////////
  322. function EncryStrHex(Str, Key: string): string;
  323. var
  324. StrResult, TempResult, Temp: string;
  325. I: Integer;
  326. begin
  327. TempResult := EncryStr(Str, Key);
  328. StrResult := '';
  329. for I := 0 to Length(TempResult) - 1 do
  330. begin
  331. Temp := Format('%x', [Ord(TempResult[I + 1])]);
  332. if Length(Temp) = 1 then Temp := '0' + Temp;
  333. StrResult := StrResult + Temp;
  334. end;
  335. Result := StrResult;
  336. end;
  337. function DESryStrHex(StrHex, Key: string): string;
  338. function HexToInt(Hex: string): Integer;
  339. var
  340. I, Res: Integer;
  341. ch: Char;
  342. begin
  343. Res := 0;
  344. for I := 0 to Length(Hex) - 1 do
  345. begin
  346. ch := Hex[I + 1];
  347. if (ch >= '0') and (ch <= '9') then
  348. Res := Res * 16 + Ord(ch) - Ord('0')
  349. else if (ch >= 'A') and (ch <= 'F') then
  350. Res := Res * 16 + Ord(ch) - Ord('A') + 10
  351. else if (ch >= 'a') and (ch <= 'f') then
  352. Res := Res * 16 + Ord(ch) - Ord('a') + 10
  353. else raise Exception.Create('Error: not a Hex String');
  354. end;
  355. Result := Res;
  356. end;
  357. var
  358. Str, Temp: string;
  359. I: Integer;
  360. begin
  361. Str := '';
  362. for I := 0 to Length(StrHex) div 2 - 1 do
  363. begin
  364. Temp := Copy(StrHex, I * 2 + 1, 2);
  365. Str := Str + Chr(HexToInt(Temp));
  366. end;
  367. Result := DESryStr(Str, Key);
  368. end;
  369. end.