PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/HGEMir3/Client??3DX/DESTR.pas

http://my-chuanqi.googlecode.com/
Pascal | 400 lines | 350 code | 47 blank | 3 comment | 64 complexity | 041d91aa8f65a2a805d134781e6df4d3 MD5 | raw file
  1. unit DESTR;
  2. interface
  3. uses
  4. SysUtils, Classes, Windows;
  5. type
  6. TKeyByte = array[0..999] of Byte;
  7. TDesMode = (dmEncry, dmDecry);
  8. function EncryStr(Str, Key: string): string;
  9. function DecryStr(Str, Key: string): string;
  10. function EncryStrHex(Str, Key: string): string;
  11. function DecryStrHex(StrHex, Key: string): string;
  12. const
  13. BitIP: array[0..63] of Byte = //????IP
  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 = //????IP-1
  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 = // ?????E
  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 = //????P
  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 = //S?
  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 = //????PC-1
  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 = //????PC-2
  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. var
  90. subKey: array[0..15] of TKeyByte;
  91. implementation
  92. procedure initPermutation(var inData: array of Byte);
  93. var
  94. newData: array[0..7] of Byte;
  95. I: Integer;
  96. begin
  97. FillChar(newData, 8, 0);
  98. for I := 0 to 63 do
  99. if (inData[BitIP[I] shr 3] and (1 shl (7 - (BitIP[I] and $07)))) <> 0 then
  100. newData[I shr 3] := newData[I shr 3] or (1 shl (7 - (I and $07)));
  101. for I := 0 to 7 do inData[I] := newData[I];
  102. end;
  103. procedure conversePermutation(var inData: array of Byte);
  104. var
  105. newData: array[0..7] of Byte;
  106. I: Integer;
  107. begin
  108. FillChar(newData, 8, 0);
  109. for I := 0 to 63 do
  110. if (inData[BitCP[I] shr 3] and (1 shl (7 - (BitCP[I] and $07)))) <> 0 then
  111. newData[I shr 3] := newData[I shr 3] or (1 shl (7 - (I and $07)));
  112. for I := 0 to 7 do inData[I] := newData[I];
  113. end;
  114. procedure Expand(inData: array of Byte; var outData: array of Byte);
  115. var
  116. I: Integer;
  117. begin
  118. FillChar(outData, 6, 0);
  119. for I := 0 to 47 do
  120. if (inData[BitExp[I] shr 3] and (1 shl (7 - (BitExp[I] and $07)))) <> 0 then
  121. outData[I shr 3] := outData[I shr 3] or (1 shl (7 - (I and $07)));
  122. end;
  123. procedure permutation(var inData: array of Byte);
  124. var
  125. newData: array[0..3] of Byte;
  126. I: Integer;
  127. begin
  128. FillChar(newData, 4, 0);
  129. for I := 0 to 31 do
  130. if (inData[BitPM[I] shr 3] and (1 shl (7 - (BitPM[I] and $07)))) <> 0 then
  131. newData[I shr 3] := newData[I shr 3] or (1 shl (7 - (I and $07)));
  132. for I := 0 to 3 do inData[I] := newData[I];
  133. end;
  134. function si(s, inByte: Byte): Byte;
  135. var
  136. c: Byte;
  137. begin
  138. c := (inByte and $20) or ((inByte and $1E) shr 1) or
  139. ((inByte and $01) shl 4);
  140. Result := (sBox[s][c] and $0F);
  141. end;
  142. procedure permutationChoose1(inData: array of Byte;
  143. var outData: array of Byte);
  144. var
  145. I: Integer;
  146. begin
  147. FillChar(outData, 7, 0);
  148. for I := 0 to 55 do
  149. if (inData[BitPMC1[I] shr 3] and (1 shl (7 - (BitPMC1[I] and $07)))) <> 0 then
  150. outData[I shr 3] := outData[I shr 3] or (1 shl (7 - (I and $07)));
  151. end;
  152. procedure permutationChoose2(inData: array of Byte;
  153. var outData: array of Byte);
  154. var
  155. I: Integer;
  156. begin
  157. FillChar(outData, 6, 0);
  158. for I := 0 to 47 do
  159. if (inData[BitPMC2[I] shr 3] and (1 shl (7 - (BitPMC2[I] and $07)))) <> 0 then
  160. outData[I shr 3] := outData[I shr 3] or (1 shl (7 - (I and $07)));
  161. end;
  162. procedure cycleMove(var inData: array of Byte; bitMove: Byte);
  163. var
  164. I: Integer;
  165. begin
  166. for I := 0 to bitMove - 1 do begin
  167. inData[0] := (inData[0] shl 1) or (inData[1] shr 7);
  168. inData[1] := (inData[1] shl 1) or (inData[2] shr 7);
  169. inData[2] := (inData[2] shl 1) or (inData[3] shr 7);
  170. inData[3] := (inData[3] shl 1) or ((inData[0] and $10) shr 4);
  171. inData[0] := (inData[0] and $0F);
  172. end;
  173. end;
  174. procedure makeKey(inKey: array of Byte; var outKey: array of TKeyByte);
  175. const
  176. bitDisplace: array[0..15] of Byte =
  177. (1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1);
  178. var
  179. outData56: array[0..6] of Byte;
  180. key28l: array[0..3] of Byte;
  181. key28r: array[0..3] of Byte;
  182. key56o: array[0..6] of Byte;
  183. I: Integer;
  184. begin
  185. permutationChoose1(inKey, outData56);
  186. key28l[0] := outData56[0] shr 4;
  187. key28l[1] := (outData56[0] shl 4) or (outData56[1] shr 4);
  188. key28l[2] := (outData56[1] shl 4) or (outData56[2] shr 4);
  189. key28l[3] := (outData56[2] shl 4) or (outData56[3] shr 4);
  190. key28r[0] := outData56[3] and $0F;
  191. key28r[1] := outData56[4];
  192. key28r[2] := outData56[5];
  193. key28r[3] := outData56[6];
  194. for I := 0 to 15 do begin
  195. cycleMove(key28l, bitDisplace[I]);
  196. cycleMove(key28r, bitDisplace[I]);
  197. key56o[0] := (key28l[0] shl 4) or (key28l[1] shr 4);
  198. key56o[1] := (key28l[1] shl 4) or (key28l[2] shr 4);
  199. key56o[2] := (key28l[2] shl 4) or (key28l[3] shr 4);
  200. key56o[3] := (key28l[3] shl 4) or (key28r[0]);
  201. key56o[4] := key28r[1];
  202. key56o[5] := key28r[2];
  203. key56o[6] := key28r[3];
  204. permutationChoose2(key56o, outKey[I]);
  205. end;
  206. end;
  207. procedure Encry(inData, subKey: array of Byte;
  208. var outData: array of Byte);
  209. var
  210. outBuf: array[0..5] of Byte;
  211. Buf: array[0..7] of Byte;
  212. I: Integer;
  213. begin
  214. Expand(inData, outBuf);
  215. for I := 0 to 5 do outBuf[I] := outBuf[I] xor subKey[I];
  216. Buf[0] := outBuf[0] shr 2;
  217. Buf[1] := ((outBuf[0] and $03) shl 4) or (outBuf[1] shr 4);
  218. Buf[2] := ((outBuf[1] and $0F) shl 2) or (outBuf[2] shr 6);
  219. Buf[3] := outBuf[2] and $3F;
  220. Buf[4] := outBuf[3] shr 2;
  221. Buf[5] := ((outBuf[3] and $03) shl 4) or (outBuf[4] shr 4);
  222. Buf[6] := ((outBuf[4] and $0F) shl 2) or (outBuf[5] shr 6);
  223. Buf[7] := outBuf[5] and $3F;
  224. for I := 0 to 7 do Buf[I] := si(I, Buf[I]);
  225. for I := 0 to 3 do outBuf[I] := (Buf[I * 2] shl 4) or Buf[I * 2 + 1];
  226. permutation(outBuf);
  227. for I := 0 to 3 do outData[I] := outBuf[I];
  228. end;
  229. procedure desData(desMode: TDesMode;
  230. inData: array of Byte; var outData: array of Byte);
  231. // inData, outData ??8Bytes?????
  232. var
  233. I, J: Integer;
  234. temp, Buf: array[0..3] of Byte;
  235. begin
  236. for I := 0 to 7 do outData[I] := inData[I];
  237. initPermutation(outData);
  238. if desMode = dmEncry then begin
  239. for I := 0 to 15 do begin
  240. for J := 0 to 3 do temp[J] := outData[J]; //temp = Ln
  241. for J := 0 to 3 do outData[J] := outData[J + 4]; //Ln+1 = Rn
  242. Encry(outData, subKey[I], Buf); //Rn ==Kn==> buf
  243. for J := 0 to 3 do outData[J + 4] := temp[J] xor Buf[J]; //Rn+1 = Ln^buf
  244. end;
  245. for J := 0 to 3 do temp[J] := outData[J + 4];
  246. for J := 0 to 3 do outData[J + 4] := outData[J];
  247. for J := 0 to 3 do outData[J] := temp[J];
  248. end
  249. else if desMode = dmDecry then begin
  250. for I := 15 downto 0 do begin
  251. for J := 0 to 3 do temp[J] := outData[J];
  252. for J := 0 to 3 do outData[J] := outData[J + 4];
  253. Encry(outData, subKey[I], Buf);
  254. for J := 0 to 3 do outData[J + 4] := temp[J] xor Buf[J];
  255. end;
  256. for J := 0 to 3 do temp[J] := outData[J + 4];
  257. for J := 0 to 3 do outData[J + 4] := outData[J];
  258. for J := 0 to 3 do outData[J] := temp[J];
  259. end;
  260. conversePermutation(outData);
  261. end;
  262. //////////////////////////////////////////////////////////////
  263. function EncryStr(Str, Key: string): string;
  264. var
  265. StrByte, OutByte, KeyByte: array[0..7] of Byte;
  266. StrResult: string;
  267. I, J: Integer;
  268. begin
  269. if (Length(Str) > 0) and (Ord(Str[Length(Str)]) = 0) then
  270. raise Exception.Create('Error: the last char is NULL char.');
  271. if Length(Key) < 8 then
  272. while Length(Key) < 8 do Key := Key + Chr(0);
  273. while Length(Str) mod 8 <> 0 do Str := Str + Chr(0);
  274. for J := 0 to 7 do KeyByte[J] := Ord(Key[J + 1]);
  275. makeKey(KeyByte, subKey);
  276. StrResult := '';
  277. for I := 0 to Length(Str) div 8 - 1 do begin
  278. for J := 0 to 7 do
  279. StrByte[J] := Ord(Str[I * 8 + J + 1]);
  280. desData(dmEncry, StrByte, OutByte);
  281. for J := 0 to 7 do
  282. StrResult := StrResult + Chr(OutByte[J]);
  283. end;
  284. Result := StrResult;
  285. end;
  286. function DecryStr(Str, Key: string): string;
  287. var
  288. StrByte, OutByte, KeyByte: array[0..7] of Byte;
  289. StrResult: string;
  290. I, J: Integer;
  291. begin
  292. if Length(Key) < 8 then
  293. while Length(Key) < 8 do Key := Key + Chr(0);
  294. for J := 0 to 7 do KeyByte[J] := Ord(Key[J + 1]);
  295. makeKey(KeyByte, subKey);
  296. StrResult := '';
  297. for I := 0 to Length(Str) div 8 - 1 do begin
  298. for J := 0 to 7 do StrByte[J] := Ord(Str[I * 8 + J + 1]);
  299. desData(dmDecry, StrByte, OutByte);
  300. for J := 0 to 7 do
  301. StrResult := StrResult + Chr(OutByte[J]);
  302. end;
  303. while (Length(StrResult) > 0) and
  304. (Ord(StrResult[Length(StrResult)]) = 0) do
  305. Delete(StrResult, Length(StrResult), 1);
  306. Result := StrResult;
  307. end;
  308. ///////////////////////////////////////////////////////////
  309. function EncryStrHex(Str, Key: string): string;
  310. var
  311. StrResult, TempResult, temp: string;
  312. I: Integer;
  313. begin
  314. TempResult := EncryStr(Str, Key);
  315. StrResult := '';
  316. for I := 0 to Length(TempResult) - 1 do begin
  317. temp := Format('%x', [Ord(TempResult[I + 1])]);
  318. if Length(temp) = 1 then temp := '0' + temp;
  319. StrResult := StrResult + temp;
  320. end;
  321. Result := StrResult;
  322. end;
  323. function DecryStrHex(StrHex, Key: string): string;
  324. function HexToInt(Hex: string): Integer;
  325. var
  326. I, Res: Integer;
  327. Ch: Char;
  328. begin
  329. Res := 0;
  330. for I := 0 to Length(Hex) - 1 do begin
  331. Ch := Hex[I + 1];
  332. if (Ch >= '0') and (Ch <= '9') then
  333. Res := Res * 16 + Ord(Ch) - Ord('0')
  334. else if (Ch >= 'A') and (Ch <= 'F') then
  335. Res := Res * 16 + Ord(Ch) - Ord('A') + 10
  336. else if (Ch >= 'a') and (Ch <= 'f') then
  337. Res := Res * 16 + Ord(Ch) - Ord('a') + 10
  338. else raise Exception.Create('Error: not a Hex String');
  339. end;
  340. Result := Res;
  341. end;
  342. var
  343. Str, temp: string;
  344. I: Integer;
  345. begin
  346. Str := '';
  347. for I := 0 to Length(StrHex) div 2 - 1 do begin
  348. temp := Copy(StrHex, I * 2 + 1, 2);
  349. Str := Str + Chr(HexToInt(temp));
  350. end;
  351. Result := DecryStr(Str, Key);
  352. end;
  353. end.