source: trunk/Packages/synapse/synacrypt.pas

Last change on this file was 84, checked in by chronos, 12 years ago
  • Stored packages in uncompressed files.
File size: 50.3 KB
Line 
1{==============================================================================|
2| Project : Ararat Synapse | 001.000.000 |
3|==============================================================================|
4| Content: Encryption support |
5|==============================================================================|
6| Copyright (c)2007, Lukas Gebauer |
7| All rights reserved. |
8| |
9| Redistribution and use in source and binary forms, with or without |
10| modification, are permitted provided that the following conditions are met: |
11| |
12| Redistributions of source code must retain the above copyright notice, this |
13| list of conditions and the following disclaimer. |
14| |
15| Redistributions in binary form must reproduce the above copyright notice, |
16| this list of conditions and the following disclaimer in the documentation |
17| and/or other materials provided with the distribution. |
18| |
19| Neither the name of Lukas Gebauer nor the names of its contributors may |
20| be used to endorse or promote products derived from this software without |
21| specific prior written permission. |
22| |
23| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
24| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
27| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
29| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
30| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
33| DAMAGE. |
34|==============================================================================|
35| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
36| Portions created by Lukas Gebauer are Copyright (c)2007. |
37| All Rights Reserved. |
38| Based on work of David Barton and Eric Young |
39|==============================================================================|
40| Contributor(s): |
41|==============================================================================|
42| History: see HISTORY.HTM from distribution package |
43| (Found at URL: http://www.ararat.cz/synapse/) |
44|==============================================================================}
45
46{:@abstract(Encryption support)
47
48Implemented are DES and 3DES encryption/decryption by ECB, CBC, CFB-8bit,
49 CFB-block, OFB and CTR methods.
50}
51
52{$IFDEF FPC}
53 {$MODE DELPHI}
54{$ENDIF}
55{$Q-}
56{$R-}
57{$H+}
58
59unit synacrypt;
60
61interface
62
63uses
64 SysUtils, Classes, synautil;
65
66type
67 {:@abstract(Implementation of common routines for 64-bit block ciphers)
68
69 Do not use this class directly, use descendants only!}
70 TSynaBlockCipher= class(TObject)
71 protected
72 procedure InitKey(Key: AnsiString); virtual;
73 private
74 IV, CV: AnsiString;
75 procedure IncCounter;
76 public
77 {:Sets the IV to Value and performs a reset}
78 procedure SetIV(const Value: AnsiString); virtual;
79 {:Returns the current chaining information, not the actual IV}
80 function GetIV: AnsiString; virtual;
81 {:Reset any stored chaining information}
82 procedure Reset; virtual;
83 {:Encrypt a 64-bit block of data using the ECB method of encryption}
84 function EncryptECB(const InData: AnsiString): AnsiString; virtual;
85 {:Decrypt a 64-bit block of data using the ECB method of decryption}
86 function DecryptECB(const InData: AnsiString): AnsiString; virtual;
87 {:Encrypt data using the CBC method of encryption}
88 function EncryptCBC(const Indata: AnsiString): AnsiString; virtual;
89 {:Decrypt data using the CBC method of decryption}
90 function DecryptCBC(const Indata: AnsiString): AnsiString; virtual;
91 {:Encrypt data using the CFB (8 bit) method of encryption}
92 function EncryptCFB8bit(const Indata: AnsiString): AnsiString; virtual;
93 {:Decrypt data using the CFB (8 bit) method of decryption}
94 function DecryptCFB8bit(const Indata: AnsiString): AnsiString; virtual;
95 {:Encrypt data using the CFB (block) method of encryption}
96 function EncryptCFBblock(const Indata: AnsiString): AnsiString; virtual;
97 {:Decrypt data using the CFB (block) method of decryption}
98 function DecryptCFBblock(const Indata: AnsiString): AnsiString; virtual;
99 {:Encrypt data using the OFB method of encryption}
100 function EncryptOFB(const Indata: AnsiString): AnsiString; virtual;
101 {:Decrypt data using the OFB method of decryption}
102 function DecryptOFB(const Indata: AnsiString): AnsiString; virtual;
103 {:Encrypt data using the CTR method of encryption}
104 function EncryptCTR(const Indata: AnsiString): AnsiString; virtual;
105 {:Decrypt data using the CTR method of decryption}
106 function DecryptCTR(const Indata: AnsiString): AnsiString; virtual;
107 {:Create a encryptor/decryptor instance and initialize it by the Key.}
108 constructor Create(Key: AnsiString);
109 end;
110
111 {:@abstract(Datatype for holding one DES key data)
112
113 This data type is used internally.}
114 TDesKeyData = array[0..31] of integer;
115
116 {:@abstract(Implementation of common routines for DES encryption)
117
118 Do not use this class directly, use descendants only!}
119 TSynaCustomDes = class(TSynaBlockcipher)
120 protected
121 procedure DoInit(KeyB: AnsiString; var KeyData: TDesKeyData);
122 function EncryptBlock(const InData: AnsiString; var KeyData: TDesKeyData): AnsiString;
123 function DecryptBlock(const InData: AnsiString; var KeyData: TDesKeyData): AnsiString;
124 end;
125
126 {:@abstract(Implementation of DES encryption)}
127 TSynaDes= class(TSynaCustomDes)
128 protected
129 KeyData: TDesKeyData;
130 procedure InitKey(Key: AnsiString); override;
131 public
132 {:Encrypt a 64-bit block of data using the ECB method of encryption}
133 function EncryptECB(const InData: AnsiString): AnsiString; override;
134 {:Decrypt a 64-bit block of data using the ECB method of decryption}
135 function DecryptECB(const InData: AnsiString): AnsiString; override;
136 end;
137
138 {:@abstract(Implementation of 3DES encryption)}
139 TSyna3Des= class(TSynaCustomDes)
140 protected
141 KeyData: array[0..2] of TDesKeyData;
142 procedure InitKey(Key: AnsiString); override;
143 public
144 {:Encrypt a 64-bit block of data using the ECB method of encryption}
145 function EncryptECB(const InData: AnsiString): AnsiString; override;
146 {:Decrypt a 64-bit block of data using the ECB method of decryption}
147 function DecryptECB(const InData: AnsiString): AnsiString; override;
148 end;
149
150{:Call internal test of all DES encryptions. Returns @true if all is OK.}
151function TestDes: boolean;
152{:Call internal test of all 3DES encryptions. Returns @true if all is OK.}
153function Test3Des: boolean;
154
155{==============================================================================}
156implementation
157
158const
159 shifts2: array[0..15]of byte=
160 (0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0);
161
162 des_skb: array[0..7,0..63]of integer=(
163 (
164 (* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 *)
165 integer($00000000),integer($00000010),integer($20000000),integer($20000010),
166 integer($00010000),integer($00010010),integer($20010000),integer($20010010),
167 integer($00000800),integer($00000810),integer($20000800),integer($20000810),
168 integer($00010800),integer($00010810),integer($20010800),integer($20010810),
169 integer($00000020),integer($00000030),integer($20000020),integer($20000030),
170 integer($00010020),integer($00010030),integer($20010020),integer($20010030),
171 integer($00000820),integer($00000830),integer($20000820),integer($20000830),
172 integer($00010820),integer($00010830),integer($20010820),integer($20010830),
173 integer($00080000),integer($00080010),integer($20080000),integer($20080010),
174 integer($00090000),integer($00090010),integer($20090000),integer($20090010),
175 integer($00080800),integer($00080810),integer($20080800),integer($20080810),
176 integer($00090800),integer($00090810),integer($20090800),integer($20090810),
177 integer($00080020),integer($00080030),integer($20080020),integer($20080030),
178 integer($00090020),integer($00090030),integer($20090020),integer($20090030),
179 integer($00080820),integer($00080830),integer($20080820),integer($20080830),
180 integer($00090820),integer($00090830),integer($20090820),integer($20090830)
181 ),(
182 (* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 *)
183 integer($00000000),integer($02000000),integer($00002000),integer($02002000),
184 integer($00200000),integer($02200000),integer($00202000),integer($02202000),
185 integer($00000004),integer($02000004),integer($00002004),integer($02002004),
186 integer($00200004),integer($02200004),integer($00202004),integer($02202004),
187 integer($00000400),integer($02000400),integer($00002400),integer($02002400),
188 integer($00200400),integer($02200400),integer($00202400),integer($02202400),
189 integer($00000404),integer($02000404),integer($00002404),integer($02002404),
190 integer($00200404),integer($02200404),integer($00202404),integer($02202404),
191 integer($10000000),integer($12000000),integer($10002000),integer($12002000),
192 integer($10200000),integer($12200000),integer($10202000),integer($12202000),
193 integer($10000004),integer($12000004),integer($10002004),integer($12002004),
194 integer($10200004),integer($12200004),integer($10202004),integer($12202004),
195 integer($10000400),integer($12000400),integer($10002400),integer($12002400),
196 integer($10200400),integer($12200400),integer($10202400),integer($12202400),
197 integer($10000404),integer($12000404),integer($10002404),integer($12002404),
198 integer($10200404),integer($12200404),integer($10202404),integer($12202404)
199 ),(
200 (* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 *)
201 integer($00000000),integer($00000001),integer($00040000),integer($00040001),
202 integer($01000000),integer($01000001),integer($01040000),integer($01040001),
203 integer($00000002),integer($00000003),integer($00040002),integer($00040003),
204 integer($01000002),integer($01000003),integer($01040002),integer($01040003),
205 integer($00000200),integer($00000201),integer($00040200),integer($00040201),
206 integer($01000200),integer($01000201),integer($01040200),integer($01040201),
207 integer($00000202),integer($00000203),integer($00040202),integer($00040203),
208 integer($01000202),integer($01000203),integer($01040202),integer($01040203),
209 integer($08000000),integer($08000001),integer($08040000),integer($08040001),
210 integer($09000000),integer($09000001),integer($09040000),integer($09040001),
211 integer($08000002),integer($08000003),integer($08040002),integer($08040003),
212 integer($09000002),integer($09000003),integer($09040002),integer($09040003),
213 integer($08000200),integer($08000201),integer($08040200),integer($08040201),
214 integer($09000200),integer($09000201),integer($09040200),integer($09040201),
215 integer($08000202),integer($08000203),integer($08040202),integer($08040203),
216 integer($09000202),integer($09000203),integer($09040202),integer($09040203)
217 ),(
218 (* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 *)
219 integer($00000000),integer($00100000),integer($00000100),integer($00100100),
220 integer($00000008),integer($00100008),integer($00000108),integer($00100108),
221 integer($00001000),integer($00101000),integer($00001100),integer($00101100),
222 integer($00001008),integer($00101008),integer($00001108),integer($00101108),
223 integer($04000000),integer($04100000),integer($04000100),integer($04100100),
224 integer($04000008),integer($04100008),integer($04000108),integer($04100108),
225 integer($04001000),integer($04101000),integer($04001100),integer($04101100),
226 integer($04001008),integer($04101008),integer($04001108),integer($04101108),
227 integer($00020000),integer($00120000),integer($00020100),integer($00120100),
228 integer($00020008),integer($00120008),integer($00020108),integer($00120108),
229 integer($00021000),integer($00121000),integer($00021100),integer($00121100),
230 integer($00021008),integer($00121008),integer($00021108),integer($00121108),
231 integer($04020000),integer($04120000),integer($04020100),integer($04120100),
232 integer($04020008),integer($04120008),integer($04020108),integer($04120108),
233 integer($04021000),integer($04121000),integer($04021100),integer($04121100),
234 integer($04021008),integer($04121008),integer($04021108),integer($04121108)
235 ),(
236 (* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 *)
237 integer($00000000),integer($10000000),integer($00010000),integer($10010000),
238 integer($00000004),integer($10000004),integer($00010004),integer($10010004),
239 integer($20000000),integer($30000000),integer($20010000),integer($30010000),
240 integer($20000004),integer($30000004),integer($20010004),integer($30010004),
241 integer($00100000),integer($10100000),integer($00110000),integer($10110000),
242 integer($00100004),integer($10100004),integer($00110004),integer($10110004),
243 integer($20100000),integer($30100000),integer($20110000),integer($30110000),
244 integer($20100004),integer($30100004),integer($20110004),integer($30110004),
245 integer($00001000),integer($10001000),integer($00011000),integer($10011000),
246 integer($00001004),integer($10001004),integer($00011004),integer($10011004),
247 integer($20001000),integer($30001000),integer($20011000),integer($30011000),
248 integer($20001004),integer($30001004),integer($20011004),integer($30011004),
249 integer($00101000),integer($10101000),integer($00111000),integer($10111000),
250 integer($00101004),integer($10101004),integer($00111004),integer($10111004),
251 integer($20101000),integer($30101000),integer($20111000),integer($30111000),
252 integer($20101004),integer($30101004),integer($20111004),integer($30111004)
253 ),(
254 (* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 *)
255 integer($00000000),integer($08000000),integer($00000008),integer($08000008),
256 integer($00000400),integer($08000400),integer($00000408),integer($08000408),
257 integer($00020000),integer($08020000),integer($00020008),integer($08020008),
258 integer($00020400),integer($08020400),integer($00020408),integer($08020408),
259 integer($00000001),integer($08000001),integer($00000009),integer($08000009),
260 integer($00000401),integer($08000401),integer($00000409),integer($08000409),
261 integer($00020001),integer($08020001),integer($00020009),integer($08020009),
262 integer($00020401),integer($08020401),integer($00020409),integer($08020409),
263 integer($02000000),integer($0A000000),integer($02000008),integer($0A000008),
264 integer($02000400),integer($0A000400),integer($02000408),integer($0A000408),
265 integer($02020000),integer($0A020000),integer($02020008),integer($0A020008),
266 integer($02020400),integer($0A020400),integer($02020408),integer($0A020408),
267 integer($02000001),integer($0A000001),integer($02000009),integer($0A000009),
268 integer($02000401),integer($0A000401),integer($02000409),integer($0A000409),
269 integer($02020001),integer($0A020001),integer($02020009),integer($0A020009),
270 integer($02020401),integer($0A020401),integer($02020409),integer($0A020409)
271 ),(
272 (* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 *)
273 integer($00000000),integer($00000100),integer($00080000),integer($00080100),
274 integer($01000000),integer($01000100),integer($01080000),integer($01080100),
275 integer($00000010),integer($00000110),integer($00080010),integer($00080110),
276 integer($01000010),integer($01000110),integer($01080010),integer($01080110),
277 integer($00200000),integer($00200100),integer($00280000),integer($00280100),
278 integer($01200000),integer($01200100),integer($01280000),integer($01280100),
279 integer($00200010),integer($00200110),integer($00280010),integer($00280110),
280 integer($01200010),integer($01200110),integer($01280010),integer($01280110),
281 integer($00000200),integer($00000300),integer($00080200),integer($00080300),
282 integer($01000200),integer($01000300),integer($01080200),integer($01080300),
283 integer($00000210),integer($00000310),integer($00080210),integer($00080310),
284 integer($01000210),integer($01000310),integer($01080210),integer($01080310),
285 integer($00200200),integer($00200300),integer($00280200),integer($00280300),
286 integer($01200200),integer($01200300),integer($01280200),integer($01280300),
287 integer($00200210),integer($00200310),integer($00280210),integer($00280310),
288 integer($01200210),integer($01200310),integer($01280210),integer($01280310)
289 ),(
290 (* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 *)
291 integer($00000000),integer($04000000),integer($00040000),integer($04040000),
292 integer($00000002),integer($04000002),integer($00040002),integer($04040002),
293 integer($00002000),integer($04002000),integer($00042000),integer($04042000),
294 integer($00002002),integer($04002002),integer($00042002),integer($04042002),
295 integer($00000020),integer($04000020),integer($00040020),integer($04040020),
296 integer($00000022),integer($04000022),integer($00040022),integer($04040022),
297 integer($00002020),integer($04002020),integer($00042020),integer($04042020),
298 integer($00002022),integer($04002022),integer($00042022),integer($04042022),
299 integer($00000800),integer($04000800),integer($00040800),integer($04040800),
300 integer($00000802),integer($04000802),integer($00040802),integer($04040802),
301 integer($00002800),integer($04002800),integer($00042800),integer($04042800),
302 integer($00002802),integer($04002802),integer($00042802),integer($04042802),
303 integer($00000820),integer($04000820),integer($00040820),integer($04040820),
304 integer($00000822),integer($04000822),integer($00040822),integer($04040822),
305 integer($00002820),integer($04002820),integer($00042820),integer($04042820),
306 integer($00002822),integer($04002822),integer($00042822),integer($04042822)
307 ));
308
309 des_sptrans: array[0..7,0..63] of integer=(
310 (
311 (* nibble 0 *)
312 integer($02080800), integer($00080000), integer($02000002), integer($02080802),
313 integer($02000000), integer($00080802), integer($00080002), integer($02000002),
314 integer($00080802), integer($02080800), integer($02080000), integer($00000802),
315 integer($02000802), integer($02000000), integer($00000000), integer($00080002),
316 integer($00080000), integer($00000002), integer($02000800), integer($00080800),
317 integer($02080802), integer($02080000), integer($00000802), integer($02000800),
318 integer($00000002), integer($00000800), integer($00080800), integer($02080002),
319 integer($00000800), integer($02000802), integer($02080002), integer($00000000),
320 integer($00000000), integer($02080802), integer($02000800), integer($00080002),
321 integer($02080800), integer($00080000), integer($00000802), integer($02000800),
322 integer($02080002), integer($00000800), integer($00080800), integer($02000002),
323 integer($00080802), integer($00000002), integer($02000002), integer($02080000),
324 integer($02080802), integer($00080800), integer($02080000), integer($02000802),
325 integer($02000000), integer($00000802), integer($00080002), integer($00000000),
326 integer($00080000), integer($02000000), integer($02000802), integer($02080800),
327 integer($00000002), integer($02080002), integer($00000800), integer($00080802)
328 ),(
329 (* nibble 1 *)
330 integer($40108010), integer($00000000), integer($00108000), integer($40100000),
331 integer($40000010), integer($00008010), integer($40008000), integer($00108000),
332 integer($00008000), integer($40100010), integer($00000010), integer($40008000),
333 integer($00100010), integer($40108000), integer($40100000), integer($00000010),
334 integer($00100000), integer($40008010), integer($40100010), integer($00008000),
335 integer($00108010), integer($40000000), integer($00000000), integer($00100010),
336 integer($40008010), integer($00108010), integer($40108000), integer($40000010),
337 integer($40000000), integer($00100000), integer($00008010), integer($40108010),
338 integer($00100010), integer($40108000), integer($40008000), integer($00108010),
339 integer($40108010), integer($00100010), integer($40000010), integer($00000000),
340 integer($40000000), integer($00008010), integer($00100000), integer($40100010),
341 integer($00008000), integer($40000000), integer($00108010), integer($40008010),
342 integer($40108000), integer($00008000), integer($00000000), integer($40000010),
343 integer($00000010), integer($40108010), integer($00108000), integer($40100000),
344 integer($40100010), integer($00100000), integer($00008010), integer($40008000),
345 integer($40008010), integer($00000010), integer($40100000), integer($00108000)
346 ),(
347 (* nibble 2 *)
348 integer($04000001), integer($04040100), integer($00000100), integer($04000101),
349 integer($00040001), integer($04000000), integer($04000101), integer($00040100),
350 integer($04000100), integer($00040000), integer($04040000), integer($00000001),
351 integer($04040101), integer($00000101), integer($00000001), integer($04040001),
352 integer($00000000), integer($00040001), integer($04040100), integer($00000100),
353 integer($00000101), integer($04040101), integer($00040000), integer($04000001),
354 integer($04040001), integer($04000100), integer($00040101), integer($04040000),
355 integer($00040100), integer($00000000), integer($04000000), integer($00040101),
356 integer($04040100), integer($00000100), integer($00000001), integer($00040000),
357 integer($00000101), integer($00040001), integer($04040000), integer($04000101),
358 integer($00000000), integer($04040100), integer($00040100), integer($04040001),
359 integer($00040001), integer($04000000), integer($04040101), integer($00000001),
360 integer($00040101), integer($04000001), integer($04000000), integer($04040101),
361 integer($00040000), integer($04000100), integer($04000101), integer($00040100),
362 integer($04000100), integer($00000000), integer($04040001), integer($00000101),
363 integer($04000001), integer($00040101), integer($00000100), integer($04040000)
364 ),(
365 (* nibble 3 *)
366 integer($00401008), integer($10001000), integer($00000008), integer($10401008),
367 integer($00000000), integer($10400000), integer($10001008), integer($00400008),
368 integer($10401000), integer($10000008), integer($10000000), integer($00001008),
369 integer($10000008), integer($00401008), integer($00400000), integer($10000000),
370 integer($10400008), integer($00401000), integer($00001000), integer($00000008),
371 integer($00401000), integer($10001008), integer($10400000), integer($00001000),
372 integer($00001008), integer($00000000), integer($00400008), integer($10401000),
373 integer($10001000), integer($10400008), integer($10401008), integer($00400000),
374 integer($10400008), integer($00001008), integer($00400000), integer($10000008),
375 integer($00401000), integer($10001000), integer($00000008), integer($10400000),
376 integer($10001008), integer($00000000), integer($00001000), integer($00400008),
377 integer($00000000), integer($10400008), integer($10401000), integer($00001000),
378 integer($10000000), integer($10401008), integer($00401008), integer($00400000),
379 integer($10401008), integer($00000008), integer($10001000), integer($00401008),
380 integer($00400008), integer($00401000), integer($10400000), integer($10001008),
381 integer($00001008), integer($10000000), integer($10000008), integer($10401000)
382 ),(
383 (* nibble 4 *)
384 integer($08000000), integer($00010000), integer($00000400), integer($08010420),
385 integer($08010020), integer($08000400), integer($00010420), integer($08010000),
386 integer($00010000), integer($00000020), integer($08000020), integer($00010400),
387 integer($08000420), integer($08010020), integer($08010400), integer($00000000),
388 integer($00010400), integer($08000000), integer($00010020), integer($00000420),
389 integer($08000400), integer($00010420), integer($00000000), integer($08000020),
390 integer($00000020), integer($08000420), integer($08010420), integer($00010020),
391 integer($08010000), integer($00000400), integer($00000420), integer($08010400),
392 integer($08010400), integer($08000420), integer($00010020), integer($08010000),
393 integer($00010000), integer($00000020), integer($08000020), integer($08000400),
394 integer($08000000), integer($00010400), integer($08010420), integer($00000000),
395 integer($00010420), integer($08000000), integer($00000400), integer($00010020),
396 integer($08000420), integer($00000400), integer($00000000), integer($08010420),
397 integer($08010020), integer($08010400), integer($00000420), integer($00010000),
398 integer($00010400), integer($08010020), integer($08000400), integer($00000420),
399 integer($00000020), integer($00010420), integer($08010000), integer($08000020)
400 ),(
401 (* nibble 5 *)
402 integer($80000040), integer($00200040), integer($00000000), integer($80202000),
403 integer($00200040), integer($00002000), integer($80002040), integer($00200000),
404 integer($00002040), integer($80202040), integer($00202000), integer($80000000),
405 integer($80002000), integer($80000040), integer($80200000), integer($00202040),
406 integer($00200000), integer($80002040), integer($80200040), integer($00000000),
407 integer($00002000), integer($00000040), integer($80202000), integer($80200040),
408 integer($80202040), integer($80200000), integer($80000000), integer($00002040),
409 integer($00000040), integer($00202000), integer($00202040), integer($80002000),
410 integer($00002040), integer($80000000), integer($80002000), integer($00202040),
411 integer($80202000), integer($00200040), integer($00000000), integer($80002000),
412 integer($80000000), integer($00002000), integer($80200040), integer($00200000),
413 integer($00200040), integer($80202040), integer($00202000), integer($00000040),
414 integer($80202040), integer($00202000), integer($00200000), integer($80002040),
415 integer($80000040), integer($80200000), integer($00202040), integer($00000000),
416 integer($00002000), integer($80000040), integer($80002040), integer($80202000),
417 integer($80200000), integer($00002040), integer($00000040), integer($80200040)
418 ),(
419 (* nibble 6 *)
420 integer($00004000), integer($00000200), integer($01000200), integer($01000004),
421 integer($01004204), integer($00004004), integer($00004200), integer($00000000),
422 integer($01000000), integer($01000204), integer($00000204), integer($01004000),
423 integer($00000004), integer($01004200), integer($01004000), integer($00000204),
424 integer($01000204), integer($00004000), integer($00004004), integer($01004204),
425 integer($00000000), integer($01000200), integer($01000004), integer($00004200),
426 integer($01004004), integer($00004204), integer($01004200), integer($00000004),
427 integer($00004204), integer($01004004), integer($00000200), integer($01000000),
428 integer($00004204), integer($01004000), integer($01004004), integer($00000204),
429 integer($00004000), integer($00000200), integer($01000000), integer($01004004),
430 integer($01000204), integer($00004204), integer($00004200), integer($00000000),
431 integer($00000200), integer($01000004), integer($00000004), integer($01000200),
432 integer($00000000), integer($01000204), integer($01000200), integer($00004200),
433 integer($00000204), integer($00004000), integer($01004204), integer($01000000),
434 integer($01004200), integer($00000004), integer($00004004), integer($01004204),
435 integer($01000004), integer($01004200), integer($01004000), integer($00004004)
436 ),(
437 (* nibble 7 *)
438 integer($20800080), integer($20820000), integer($00020080), integer($00000000),
439 integer($20020000), integer($00800080), integer($20800000), integer($20820080),
440 integer($00000080), integer($20000000), integer($00820000), integer($00020080),
441 integer($00820080), integer($20020080), integer($20000080), integer($20800000),
442 integer($00020000), integer($00820080), integer($00800080), integer($20020000),
443 integer($20820080), integer($20000080), integer($00000000), integer($00820000),
444 integer($20000000), integer($00800000), integer($20020080), integer($20800080),
445 integer($00800000), integer($00020000), integer($20820000), integer($00000080),
446 integer($00800000), integer($00020000), integer($20000080), integer($20820080),
447 integer($00020080), integer($20000000), integer($00000000), integer($00820000),
448 integer($20800080), integer($20020080), integer($20020000), integer($00800080),
449 integer($20820000), integer($00000080), integer($00800080), integer($20020000),
450 integer($20820080), integer($00800000), integer($20800000), integer($20000080),
451 integer($00820000), integer($00020080), integer($20020080), integer($20800000),
452 integer($00000080), integer($20820000), integer($00820080), integer($00000000),
453 integer($20000000), integer($20800080), integer($00020000), integer($00820080)
454 ));
455
456{==============================================================================}
457
458function XorString(Indata1, Indata2: AnsiString): AnsiString;
459var
460 i: integer;
461begin
462 Indata2 := PadString(Indata2, length(Indata1), #0);
463 Result := '';
464 for i := 1 to length(Indata1) do
465 Result := Result + AnsiChar(ord(Indata1[i]) xor ord(Indata2[i]));
466end;
467
468procedure hperm_op(var a, t: integer; n, m: integer);
469begin
470 t:= ((a shl (16 - n)) xor a) and m;
471 a:= a xor t xor (t shr (16 - n));
472end;
473
474procedure perm_op(var a, b, t: integer; n, m: integer);
475begin
476 t:= ((a shr n) xor b) and m;
477 b:= b xor t;
478 a:= a xor (t shl n);
479end;
480
481{==============================================================================}
482procedure TSynaBlockCipher.IncCounter;
483var
484 i: integer;
485begin
486 Inc(CV[8]);
487 i:= 7;
488 while (i> 0) and (CV[i + 1] = #0) do
489 begin
490 Inc(CV[i]);
491 Dec(i);
492 end;
493end;
494
495procedure TSynaBlockCipher.Reset;
496begin
497 CV := IV;
498end;
499
500procedure TSynaBlockCipher.InitKey(Key: AnsiString);
501begin
502end;
503
504procedure TSynaBlockCipher.SetIV(const Value: AnsiString);
505begin
506 IV := PadString(Value, 8, #0);
507 Reset;
508end;
509
510function TSynaBlockCipher.GetIV: AnsiString;
511begin
512 Result := CV;
513end;
514
515function TSynaBlockCipher.EncryptECB(const InData: AnsiString): AnsiString;
516begin
517 Result := InData;
518end;
519
520function TSynaBlockCipher.DecryptECB(const InData: AnsiString): AnsiString;
521begin
522 Result := InData;
523end;
524
525function TSynaBlockCipher.EncryptCBC(const Indata: AnsiString): AnsiString;
526var
527 i: integer;
528 s: ansistring;
529 l: integer;
530begin
531 Result := '';
532 l := Length(InData);
533 for i:= 1 to (l div 8) do
534 begin
535 s := copy(Indata, (i - 1) * 8 + 1, 8);
536 s := XorString(s, CV);
537 s := EncryptECB(s);
538 CV := s;
539 Result := Result + s;
540 end;
541 if (l mod 8)<> 0 then
542 begin
543 CV := EncryptECB(CV);
544 s := copy(Indata, (l div 8) * 8 + 1, l mod 8);
545 s := XorString(s, CV);
546 Result := Result + s;
547 end;
548end;
549
550function TSynaBlockCipher.DecryptCBC(const Indata: AnsiString): AnsiString;
551var
552 i: integer;
553 s, temp: ansistring;
554 l: integer;
555begin
556 Result := '';
557 l := Length(InData);
558 for i:= 1 to (l div 8) do
559 begin
560 s := copy(Indata, (i - 1) * 8 + 1, 8);
561 temp := s;
562 s := DecryptECB(s);
563 s := XorString(s, CV);
564 Result := Result + s;
565 CV := Temp;
566 end;
567 if (l mod 8)<> 0 then
568 begin
569 CV := EncryptECB(CV);
570 s := copy(Indata, (l div 8) * 8 + 1, l mod 8);
571 s := XorString(s, CV);
572 Result := Result + s;
573 end;
574end;
575
576function TSynaBlockCipher.EncryptCFB8bit(const Indata: AnsiString): AnsiString;
577var
578 i: integer;
579 Temp: AnsiString;
580 c: AnsiChar;
581begin
582 Result := '';
583 for i:= 1 to Length(Indata) do
584 begin
585 Temp := EncryptECB(CV);
586 c := AnsiChar(ord(InData[i]) xor ord(temp[1]));
587 Result := Result + c;
588 Delete(CV, 1, 1);
589 CV := CV + c;
590 end;
591end;
592
593function TSynaBlockCipher.DecryptCFB8bit(const Indata: AnsiString): AnsiString;
594var
595 i: integer;
596 Temp: AnsiString;
597 c: AnsiChar;
598begin
599 Result := '';
600 for i:= 1 to length(Indata) do
601 begin
602 c:= Indata[i];
603 Temp := EncryptECB(CV);
604 Result := Result + AnsiChar(ord(InData[i]) xor ord(temp[1]));
605 Delete(CV, 1, 1);
606 CV := CV + c;
607 end;
608end;
609
610function TSynaBlockCipher.EncryptCFBblock(const Indata: AnsiString): AnsiString;
611var
612 i: integer;
613 s: AnsiString;
614 l: integer;
615begin
616 Result := '';
617 l := Length(InData);
618 for i:= 1 to (l div 8) do
619 begin
620 CV := EncryptECB(CV);
621 s := copy(Indata, (i - 1) * 8 + 1, 8);
622 s := XorString(s, CV);
623 Result := Result + s;
624 CV := s;
625 end;
626 if (l mod 8)<> 0 then
627 begin
628 CV := EncryptECB(CV);
629 s := copy(Indata, (l div 8) * 8 + 1, l mod 8);
630 s := XorString(s, CV);
631 Result := Result + s;
632 end;
633end;
634
635function TSynaBlockCipher.DecryptCFBblock(const Indata: AnsiString): AnsiString;
636var
637 i: integer;
638 S, Temp: AnsiString;
639 l: integer;
640begin
641 Result := '';
642 l := Length(InData);
643 for i:= 1 to (l div 8) do
644 begin
645 s := copy(Indata, (i - 1) * 8 + 1, 8);
646 Temp := s;
647 CV := EncryptECB(CV);
648 s := XorString(s, CV);
649 Result := result + s;
650 CV := temp;
651 end;
652 if (l mod 8)<> 0 then
653 begin
654 CV := EncryptECB(CV);
655 s := copy(Indata, (l div 8) * 8 + 1, l mod 8);
656 s := XorString(s, CV);
657 Result := Result + s;
658 end;
659end;
660
661function TSynaBlockCipher.EncryptOFB(const Indata: AnsiString): AnsiString;
662var
663 i: integer;
664 s: AnsiString;
665 l: integer;
666begin
667 Result := '';
668 l := Length(InData);
669 for i:= 1 to (l div 8) do
670 begin
671 CV := EncryptECB(CV);
672 s := copy(Indata, (i - 1) * 8 + 1, 8);
673 s := XorString(s, CV);
674 Result := Result + s;
675 end;
676 if (l mod 8)<> 0 then
677 begin
678 CV := EncryptECB(CV);
679 s := copy(Indata, (l div 8) * 8 + 1, l mod 8);
680 s := XorString(s, CV);
681 Result := Result + s;
682 end;
683end;
684
685function TSynaBlockCipher.DecryptOFB(const Indata: AnsiString): AnsiString;
686var
687 i: integer;
688 s: AnsiString;
689 l: integer;
690begin
691 Result := '';
692 l := Length(InData);
693 for i:= 1 to (l div 8) do
694 begin
695 Cv := EncryptECB(CV);
696 s := copy(Indata, (i - 1) * 8 + 1, 8);
697 s := XorString(s, CV);
698 Result := Result + s;
699 end;
700 if (l mod 8)<> 0 then
701 begin
702 CV := EncryptECB(CV);
703 s := copy(Indata, (l div 8) * 8 + 1, l mod 8);
704 s := XorString(s, CV);
705 Result := Result + s;
706 end;
707end;
708
709function TSynaBlockCipher.EncryptCTR(const Indata: AnsiString): AnsiString;
710var
711 temp: AnsiString;
712 i: integer;
713 s: AnsiString;
714 l: integer;
715begin
716 Result := '';
717 l := Length(InData);
718 for i:= 1 to (l div 8) do
719 begin
720 temp := EncryptECB(CV);
721 IncCounter;
722 s := copy(Indata, (i - 1) * 8 + 1, 8);
723 s := XorString(s, temp);
724 Result := Result + s;
725 end;
726 if (l mod 8)<> 0 then
727 begin
728 temp := EncryptECB(CV);
729 IncCounter;
730 s := copy(Indata, (l div 8) * 8 + 1, l mod 8);
731 s := XorString(s, temp);
732 Result := Result + s;
733 end;
734end;
735
736function TSynaBlockCipher.DecryptCTR(const Indata: AnsiString): AnsiString;
737var
738 temp: AnsiString;
739 s: AnsiString;
740 i: integer;
741 l: integer;
742begin
743 Result := '';
744 l := Length(InData);
745 for i:= 1 to (l div 8) do
746 begin
747 temp := EncryptECB(CV);
748 IncCounter;
749 s := copy(Indata, (i - 1) * 8 + 1, 8);
750 s := XorString(s, temp);
751 Result := Result + s;
752 end;
753 if (l mod 8)<> 0 then
754 begin
755 temp := EncryptECB(CV);
756 IncCounter;
757 s := copy(Indata, (l div 8) * 8 + 1, l mod 8);
758 s := XorString(s, temp);
759 Result := Result + s;
760 end;
761end;
762
763constructor TSynaBlockCipher.Create(Key: AnsiString);
764begin
765 inherited Create;
766 InitKey(Key);
767 IV := StringOfChar(#0, 8);
768 IV := EncryptECB(IV);
769 Reset;
770end;
771
772{==============================================================================}
773
774procedure TSynaCustomDes.DoInit(KeyB: AnsiString; var KeyData: TDesKeyData);
775var
776 c, d, t, s, t2, i: integer;
777begin
778 KeyB := PadString(KeyB, 8, #0);
779 c:= ord(KeyB[1]) or (ord(KeyB[2]) shl 8) or (ord(KeyB[3]) shl 16) or (ord(KeyB[4]) shl 24);
780 d:= ord(KeyB[5]) or (ord(KeyB[6]) shl 8) or (ord(KeyB[7]) shl 16) or (ord(KeyB[8]) shl 24);
781 perm_op(d,c,t,4,integer($0f0f0f0f));
782 hperm_op(c,t,integer(-2),integer($cccc0000));
783 hperm_op(d,t,integer(-2),integer($cccc0000));
784 perm_op(d,c,t,1,integer($55555555));
785 perm_op(c,d,t,8,integer($00ff00ff));
786 perm_op(d,c,t,1,integer($55555555));
787 d:= ((d and $ff) shl 16) or (d and $ff00) or ((d and $ff0000) shr 16) or
788 ((c and integer($f0000000)) shr 4);
789 c:= c and $fffffff;
790 for i:= 0 to 15 do
791 begin
792 if shifts2[i]<> 0 then
793 begin
794 c:= ((c shr 2) or (c shl 26));
795 d:= ((d shr 2) or (d shl 26));
796 end
797 else
798 begin
799 c:= ((c shr 1) or (c shl 27));
800 d:= ((d shr 1) or (d shl 27));
801 end;
802 c:= c and $fffffff;
803 d:= d and $fffffff;
804 s:= des_skb[0,c and $3f] or
805 des_skb[1,((c shr 6) and $03) or ((c shr 7) and $3c)] or
806 des_skb[2,((c shr 13) and $0f) or ((c shr 14) and $30)] or
807 des_skb[3,((c shr 20) and $01) or ((c shr 21) and $06) or ((c shr 22) and $38)];
808 t:= des_skb[4,d and $3f] or
809 des_skb[5,((d shr 7) and $03) or ((d shr 8) and $3c)] or
810 des_skb[6, (d shr 15) and $3f ] or
811 des_skb[7,((d shr 21) and $0f) or ((d shr 22) and $30)];
812 t2:= ((t shl 16) or (s and $ffff));
813 KeyData[(i shl 1)]:= ((t2 shl 2) or (t2 shr 30));
814 t2:= ((s shr 16) or (t and integer($ffff0000)));
815 KeyData[(i shl 1)+1]:= ((t2 shl 6) or (t2 shr 26));
816 end;
817end;
818
819function TSynaCustomDes.EncryptBlock(const InData: AnsiString; var KeyData: TDesKeyData): AnsiString;
820var
821 l, r, t, u: integer;
822 i: longint;
823begin
824 r := Swapbytes(DecodeLongint(Indata, 1));
825 l := swapbytes(DecodeLongint(Indata, 5));
826 t:= ((l shr 4) xor r) and $0f0f0f0f;
827 r:= r xor t;
828 l:= l xor (t shl 4);
829 t:= ((r shr 16) xor l) and $0000ffff;
830 l:= l xor t;
831 r:= r xor (t shl 16);
832 t:= ((l shr 2) xor r) and $33333333;
833 r:= r xor t;
834 l:= l xor (t shl 2);
835 t:= ((r shr 8) xor l) and $00ff00ff;
836 l:= l xor t;
837 r:= r xor (t shl 8);
838 t:= ((l shr 1) xor r) and $55555555;
839 r:= r xor t;
840 l:= l xor (t shl 1);
841 r:= (r shr 29) or (r shl 3);
842 l:= (l shr 29) or (l shl 3);
843 i:= 0;
844 while i< 32 do
845 begin
846 u:= r xor KeyData[i ];
847 t:= r xor KeyData[i+1];
848 t:= (t shr 4) or (t shl 28);
849 l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
850 des_SPtrans[2,(u shr 10) and $3f] xor
851 des_SPtrans[4,(u shr 18) and $3f] xor
852 des_SPtrans[6,(u shr 26) and $3f] xor
853 des_SPtrans[1,(t shr 2) and $3f] xor
854 des_SPtrans[3,(t shr 10) and $3f] xor
855 des_SPtrans[5,(t shr 18) and $3f] xor
856 des_SPtrans[7,(t shr 26) and $3f];
857 u:= l xor KeyData[i+2];
858 t:= l xor KeyData[i+3];
859 t:= (t shr 4) or (t shl 28);
860 r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
861 des_SPtrans[2,(u shr 10) and $3f] xor
862 des_SPtrans[4,(u shr 18) and $3f] xor
863 des_SPtrans[6,(u shr 26) and $3f] xor
864 des_SPtrans[1,(t shr 2) and $3f] xor
865 des_SPtrans[3,(t shr 10) and $3f] xor
866 des_SPtrans[5,(t shr 18) and $3f] xor
867 des_SPtrans[7,(t shr 26) and $3f];
868 u:= r xor KeyData[i+4];
869 t:= r xor KeyData[i+5];
870 t:= (t shr 4) or (t shl 28);
871 l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
872 des_SPtrans[2,(u shr 10) and $3f] xor
873 des_SPtrans[4,(u shr 18) and $3f] xor
874 des_SPtrans[6,(u shr 26) and $3f] xor
875 des_SPtrans[1,(t shr 2) and $3f] xor
876 des_SPtrans[3,(t shr 10) and $3f] xor
877 des_SPtrans[5,(t shr 18) and $3f] xor
878 des_SPtrans[7,(t shr 26) and $3f];
879 u:= l xor KeyData[i+6];
880 t:= l xor KeyData[i+7];
881 t:= (t shr 4) or (t shl 28);
882 r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
883 des_SPtrans[2,(u shr 10) and $3f] xor
884 des_SPtrans[4,(u shr 18) and $3f] xor
885 des_SPtrans[6,(u shr 26) and $3f] xor
886 des_SPtrans[1,(t shr 2) and $3f] xor
887 des_SPtrans[3,(t shr 10) and $3f] xor
888 des_SPtrans[5,(t shr 18) and $3f] xor
889 des_SPtrans[7,(t shr 26) and $3f];
890 Inc(i,8);
891 end;
892 r:= (r shr 3) or (r shl 29);
893 l:= (l shr 3) or (l shl 29);
894 t:= ((r shr 1) xor l) and $55555555;
895 l:= l xor t;
896 r:= r xor (t shl 1);
897 t:= ((l shr 8) xor r) and $00ff00ff;
898 r:= r xor t;
899 l:= l xor (t shl 8);
900 t:= ((r shr 2) xor l) and $33333333;
901 l:= l xor t;
902 r:= r xor (t shl 2);
903 t:= ((l shr 16) xor r) and $0000ffff;
904 r:= r xor t;
905 l:= l xor (t shl 16);
906 t:= ((r shr 4) xor l) and $0f0f0f0f;
907 l:= l xor t;
908 r:= r xor (t shl 4);
909 Result := CodeLongInt(Swapbytes(l)) + CodeLongInt(Swapbytes(r));
910end;
911
912function TSynaCustomDes.DecryptBlock(const InData: AnsiString; var KeyData: TDesKeyData): AnsiString;
913var
914 l, r, t, u: integer;
915 i: longint;
916begin
917 r := Swapbytes(DecodeLongint(Indata, 1));
918 l := Swapbytes(DecodeLongint(Indata, 5));
919 t:= ((l shr 4) xor r) and $0f0f0f0f;
920 r:= r xor t;
921 l:= l xor (t shl 4);
922 t:= ((r shr 16) xor l) and $0000ffff;
923 l:= l xor t;
924 r:= r xor (t shl 16);
925 t:= ((l shr 2) xor r) and $33333333;
926 r:= r xor t;
927 l:= l xor (t shl 2);
928 t:= ((r shr 8) xor l) and $00ff00ff;
929 l:= l xor t;
930 r:= r xor (t shl 8);
931 t:= ((l shr 1) xor r) and $55555555;
932 r:= r xor t;
933 l:= l xor (t shl 1);
934 r:= (r shr 29) or (r shl 3);
935 l:= (l shr 29) or (l shl 3);
936 i:= 30;
937 while i> 0 do
938 begin
939 u:= r xor KeyData[i ];
940 t:= r xor KeyData[i+1];
941 t:= (t shr 4) or (t shl 28);
942 l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
943 des_SPtrans[2,(u shr 10) and $3f] xor
944 des_SPtrans[4,(u shr 18) and $3f] xor
945 des_SPtrans[6,(u shr 26) and $3f] xor
946 des_SPtrans[1,(t shr 2) and $3f] xor
947 des_SPtrans[3,(t shr 10) and $3f] xor
948 des_SPtrans[5,(t shr 18) and $3f] xor
949 des_SPtrans[7,(t shr 26) and $3f];
950 u:= l xor KeyData[i-2];
951 t:= l xor KeyData[i-1];
952 t:= (t shr 4) or (t shl 28);
953 r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
954 des_SPtrans[2,(u shr 10) and $3f] xor
955 des_SPtrans[4,(u shr 18) and $3f] xor
956 des_SPtrans[6,(u shr 26) and $3f] xor
957 des_SPtrans[1,(t shr 2) and $3f] xor
958 des_SPtrans[3,(t shr 10) and $3f] xor
959 des_SPtrans[5,(t shr 18) and $3f] xor
960 des_SPtrans[7,(t shr 26) and $3f];
961 u:= r xor KeyData[i-4];
962 t:= r xor KeyData[i-3];
963 t:= (t shr 4) or (t shl 28);
964 l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
965 des_SPtrans[2,(u shr 10) and $3f] xor
966 des_SPtrans[4,(u shr 18) and $3f] xor
967 des_SPtrans[6,(u shr 26) and $3f] xor
968 des_SPtrans[1,(t shr 2) and $3f] xor
969 des_SPtrans[3,(t shr 10) and $3f] xor
970 des_SPtrans[5,(t shr 18) and $3f] xor
971 des_SPtrans[7,(t shr 26) and $3f];
972 u:= l xor KeyData[i-6];
973 t:= l xor KeyData[i-5];
974 t:= (t shr 4) or (t shl 28);
975 r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
976 des_SPtrans[2,(u shr 10) and $3f] xor
977 des_SPtrans[4,(u shr 18) and $3f] xor
978 des_SPtrans[6,(u shr 26) and $3f] xor
979 des_SPtrans[1,(t shr 2) and $3f] xor
980 des_SPtrans[3,(t shr 10) and $3f] xor
981 des_SPtrans[5,(t shr 18) and $3f] xor
982 des_SPtrans[7,(t shr 26) and $3f];
983 Dec(i,8);
984 end;
985 r:= (r shr 3) or (r shl 29);
986 l:= (l shr 3) or (l shl 29);
987 t:= ((r shr 1) xor l) and $55555555;
988 l:= l xor t;
989 r:= r xor (t shl 1);
990 t:= ((l shr 8) xor r) and $00ff00ff;
991 r:= r xor t;
992 l:= l xor (t shl 8);
993 t:= ((r shr 2) xor l) and $33333333;
994 l:= l xor t;
995 r:= r xor (t shl 2);
996 t:= ((l shr 16) xor r) and $0000ffff;
997 r:= r xor t;
998 l:= l xor (t shl 16);
999 t:= ((r shr 4) xor l) and $0f0f0f0f;
1000 l:= l xor t;
1001 r:= r xor (t shl 4);
1002 Result := CodeLongInt(Swapbytes(l)) + CodeLongInt(Swapbytes(r));
1003end;
1004
1005{==============================================================================}
1006
1007procedure TSynaDes.InitKey(Key: AnsiString);
1008begin
1009 Key := PadString(Key, 8, #0);
1010 DoInit(Key,KeyData);
1011end;
1012
1013function TSynaDes.EncryptECB(const InData: AnsiString): AnsiString;
1014begin
1015 Result := EncryptBlock(InData,KeyData);
1016end;
1017
1018function TSynaDes.DecryptECB(const InData: AnsiString): AnsiString;
1019begin
1020 Result := DecryptBlock(Indata,KeyData);
1021end;
1022
1023{==============================================================================}
1024
1025procedure TSyna3Des.InitKey(Key: AnsiString);
1026var
1027 Size: integer;
1028 n: integer;
1029begin
1030 Size := length(Key);
1031 key := PadString(key, 3 * 8, #0);
1032 DoInit(Copy(key, 1, 8),KeyData[0]);
1033 DoInit(Copy(key, 9, 8),KeyData[1]);
1034 if Size > 16 then
1035 DoInit(Copy(key, 17, 8),KeyData[2])
1036 else
1037 for n := 0 to high(KeyData[0]) do
1038 KeyData[2][n] := Keydata[0][n];
1039end;
1040
1041function TSyna3Des.EncryptECB(const InData: AnsiString): AnsiString;
1042begin
1043 Result := EncryptBlock(Indata,KeyData[0]);
1044 Result := DecryptBlock(Result,KeyData[1]);
1045 Result := EncryptBlock(Result,KeyData[2]);
1046end;
1047
1048function TSyna3Des.DecryptECB(const InData: AnsiString): AnsiString;
1049begin
1050 Result := DecryptBlock(InData,KeyData[2]);
1051 Result := EncryptBlock(Result,KeyData[1]);
1052 Result := DecryptBlock(Result,KeyData[0]);
1053end;
1054
1055{==============================================================================}
1056
1057function TestDes: boolean;
1058var
1059 des: TSynaDes;
1060 s, t: string;
1061const
1062 key = '01234567';
1063 data1= '01234567';
1064 data2= '0123456789abcdefghij';
1065begin
1066 //ECB
1067 des := TSynaDes.Create(key);
1068 try
1069 s := des.EncryptECB(data1);
1070 t := strtohex(s);
1071 result := t = 'c50ad028c6da9800';
1072 s := des.DecryptECB(s);
1073 result := result and (data1 = s);
1074 finally
1075 des.free;
1076 end;
1077 //CBC
1078 des := TSynaDes.Create(key);
1079 try
1080 s := des.EncryptCBC(data2);
1081 t := strtohex(s);
1082 result := result and (t = 'eec50f6353115ad6dee90a22ed1b6a88a0926e35');
1083 des.Reset;
1084 s := des.DecryptCBC(s);
1085 result := result and (data2 = s);
1086 finally
1087 des.free;
1088 end;
1089 //CFB-8bit
1090 des := TSynaDes.Create(key);
1091 try
1092 s := des.EncryptCFB8bit(data2);
1093 t := strtohex(s);
1094 result := result and (t = 'eb6aa12c2f0ff634b4dfb6da6cb2af8f9c5c1452');
1095 des.Reset;
1096 s := des.DecryptCFB8bit(s);
1097 result := result and (data2 = s);
1098 finally
1099 des.free;
1100 end;
1101 //CFB-block
1102 des := TSynaDes.Create(key);
1103 try
1104 s := des.EncryptCFBblock(data2);
1105 t := strtohex(s);
1106 result := result and (t = 'ebdbbaa7f9286cdec28605e07f9b7f3be1053257');
1107 des.Reset;
1108 s := des.DecryptCFBblock(s);
1109 result := result and (data2 = s);
1110 finally
1111 des.free;
1112 end;
1113 //OFB
1114 des := TSynaDes.Create(key);
1115 try
1116 s := des.EncryptOFB(data2);
1117 t := strtohex(s);
1118 result := result and (t = 'ebdbbaa7f9286cdee0b8b3798c4c34baac87dbdc');
1119 des.Reset;
1120 s := des.DecryptOFB(s);
1121 result := result and (data2 = s);
1122 finally
1123 des.free;
1124 end;
1125 //CTR
1126 des := TSynaDes.Create(key);
1127 try
1128 s := des.EncryptCTR(data2);
1129 t := strtohex(s);
1130 result := result and (t = 'ebdbbaa7f9286cde0dd20b45f3afd9aa1b91b87e');
1131 des.Reset;
1132 s := des.DecryptCTR(s);
1133 result := result and (data2 = s);
1134 finally
1135 des.free;
1136 end;
1137end;
1138
1139function Test3Des: boolean;
1140var
1141 des: TSyna3Des;
1142 s, t: string;
1143const
1144 key = '0123456789abcdefghijklmn';
1145 data1= '01234567';
1146 data2= '0123456789abcdefghij';
1147begin
1148 //ECB
1149 des := TSyna3Des.Create(key);
1150 try
1151 s := des.EncryptECB(data1);
1152 t := strtohex(s);
1153 result := t = 'e0dee91008dc460c';
1154 s := des.DecryptECB(s);
1155 result := result and (data1 = s);
1156 finally
1157 des.free;
1158 end;
1159 //CBC
1160 des := TSyna3Des.Create(key);
1161 try
1162 s := des.EncryptCBC(data2);
1163 t := strtohex(s);
1164 result := result and (t = 'ee844a2a4f49c01b91a1599b8eba29128c1ad87a');
1165 des.Reset;
1166 s := des.DecryptCBC(s);
1167 result := result and (data2 = s);
1168 finally
1169 des.free;
1170 end;
1171 //CFB-8bit
1172 des := TSyna3Des.Create(key);
1173 try
1174 s := des.EncryptCFB8bit(data2);
1175 t := strtohex(s);
1176 result := result and (t = '935bbf5210c32cfa1faf61f91e8dc02dfa0ff1e8');
1177 des.Reset;
1178 s := des.DecryptCFB8bit(s);
1179 result := result and (data2 = s);
1180 finally
1181 des.free;
1182 end;
1183 //CFB-block
1184 des := TSyna3Des.Create(key);
1185 try
1186 s := des.EncryptCFBblock(data2);
1187 t := strtohex(s);
1188 result := result and (t = '93754e3d54828fbf4bd81f1739419e8d2cfe1671');
1189 des.Reset;
1190 s := des.DecryptCFBblock(s);
1191 result := result and (data2 = s);
1192 finally
1193 des.free;
1194 end;
1195 //OFB
1196 des := TSyna3Des.Create(key);
1197 try
1198 s := des.EncryptOFB(data2);
1199 t := strtohex(s);
1200 result := result and (t = '93754e3d54828fbf04ef0a5efc926ebdf2d95f20');
1201 des.Reset;
1202 s := des.DecryptOFB(s);
1203 result := result and (data2 = s);
1204 finally
1205 des.free;
1206 end;
1207 //CTR
1208 des := TSyna3Des.Create(key);
1209 try
1210 s := des.EncryptCTR(data2);
1211 t := strtohex(s);
1212 result := result and (t = '93754e3d54828fbf1c51a121d2c93f989e70b3ad');
1213 des.Reset;
1214 s := des.DecryptCTR(s);
1215 result := result and (data2 = s);
1216 finally
1217 des.free;
1218 end;
1219end;
1220
1221{==============================================================================}
1222
1223end.
Note: See TracBrowser for help on using the repository browser.