-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Olf.RTL.CryptDecrypt.pas
697 lines (591 loc) · 18 KB
/
Olf.RTL.CryptDecrypt.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
unit Olf.RTL.CryptDecrypt;
interface
uses
System.Classes,
System.Types;
type
/// <summary>
/// Simple class to crypt and decrypt buffers.
/// </summary>
/// <remarks>
/// This unit contains very simple cryptographic algorithms.
/// DON't USE THEM FOR SENSIBLE DATAS !!!
///
/// Check those projects if you need a more robust algorithm :
///
/// - Delphi Encryption Compendium
/// https://github.com/MHumm/DelphiEncryptionCompendium
///
/// - TMS Cryptographic Pack
/// https://www.tmssoftware.com/site/tmscrypto.asp
///
/// </remarks>
TOlfCryptDecrypt = class
private
FIntegerKeys: TIntegerDynArray;
FByteKeys: TByteDynArray;
procedure SetByteKeys(const Value: TByteDynArray);
procedure SetIntegerKeys(const Value: TIntegerDynArray);
protected
public
/// <summary>
/// buffer of bytes used as a key by Crypt/Decrypt functions
/// </summary>
property ByteKeys: TByteDynArray read FByteKeys write SetByteKeys;
/// <summary>
/// buffer of integers used as a key by Crypt/Decrypt functions
/// </summary>
property IntegerKeys: TIntegerDynArray read FIntegerKeys
write SetIntegerKeys;
/// <summary>
/// use XOR operand to crypt a buffer with the keys buffer property
/// </summary>
/// <remarks>
/// You can use the program at https://xorkeysgenerator.olfsoftware.fr/ to
/// generate a compatible XOR keys buffer.
/// </remarks>
function XORCrypt(Const AStream: TStream): TMemoryStream; overload;
/// <summary>
/// use XOR operand to crypt a buffer with a keys buffer
/// </summary>
/// <remarks>
/// You can use the program at https://xorkeysgenerator.olfsoftware.fr/ to
/// generate a compatible XOR keys buffer.
/// </remarks>
class function XORCrypt(Const AStream: TStream; Const AKeys: TByteDynArray)
: TMemoryStream; overload;
/// <summary>
/// use XOR operand to decrypt a buffer with the keys buffer property
/// </summary>
/// <remarks>
/// You can use the program at https://xorkeysgenerator.olfsoftware.fr/ to
/// generate a compatible XOR keys buffer.
/// </remarks>
function XORDecrypt(Const AStream: TStream): TMemoryStream; overload;
/// <summary>
/// use XOR operand to decrypt a buffer with a keys buffer
/// </summary>
/// <remarks>
/// You can use the program at https://xorkeysgenerator.olfsoftware.fr/ to
/// generate a compatible XOR keys buffer.
/// </remarks>
class function XORDecrypt(Const AStream: TStream;
Const AKeys: TByteDynArray): TMemoryStream; overload;
/// <summary>
/// Export a key as an array of random bytes
/// </summary>
class function GenXORKey(Const Size: word): TByteDynArray;
/// <summary>
/// exchange bytes between a buffer to crypt and the property key
/// </summary>
/// <remarks>
/// You can use the program at https://swapkeysgenerator.olfsoftware.fr/ to
/// generate a compatible Swap keys buffer.
/// </remarks>
function SwapCrypt(Const AStream: TStream): TMemoryStream; overload;
/// <summary>
/// exchange bytes between a buffer to crypt and the key parameter
/// </summary>
/// <remarks>
/// You can use the program at https://swapkeysgenerator.olfsoftware.fr/ to
/// generate a compatible Swap keys buffer.
/// </remarks>
class function SwapCrypt(Const AStream: TStream; Const AKeys: TByteDynArray)
: TMemoryStream; overload;
/// <summary>
/// exchange bytes between a buffer to uncrypt and the property key
/// </summary>
/// <remarks>
/// You can use the program at https://swapkeysgenerator.olfsoftware.fr/ to
/// generate a compatible Swap keys buffer.
/// </remarks>
function SwapDecrypt(Const AStream: TStream): TMemoryStream; overload;
/// <summary>
/// exchange bytes between a buffer to uncrypt and the key parameter
/// </summary>
/// <remarks>
/// You can use the program at https://swapkeysgenerator.olfsoftware.fr/ to
/// generate a compatible Swap keys buffer.
/// </remarks>
class function SwapDecrypt(Const AStream: TStream;
Const AKeys: TByteDynArray): TMemoryStream; overload;
/// <summary>
/// Export a key as an array of 256 random bytes
/// </summary>
class function GenSwapKey: TByteDynArray;
function ShiftCrypt(Const AStream: TStream): TMemoryStream; overload;
class function ShiftCrypt(Const AStream: TStream;
const AKeys: TIntegerDynArray): TMemoryStream; overload;
function ShiftDecrypt(Const AStream: TStream): TMemoryStream; overload;
class function ShiftDecrypt(Const AStream: TStream;
const AKeys: TIntegerDynArray): TMemoryStream; overload;
class function GenShiftKey(Const Size: word): TIntegerDynArray;
function IDBCrypt(Const AStream: TStream): TMemoryStream; overload;
class function IDBCrypt(Const AStream: TStream;
const AKeys: TIntegerDynArray): TMemoryStream; overload;
function IDBDecrypt(Const AStream: TStream): TMemoryStream; overload;
class function IDBDecrypt(Const AStream: TStream;
const AKeys: TIntegerDynArray): TMemoryStream; overload;
class function GenIDBKey(Const Size: word): TIntegerDynArray;
/// <summary>
/// Create an instance of TOlfCryptDecrypt class
/// </summary>
constructor Create; overload;
/// <summary>
/// Create an instance of TOlfCryptDecrypt class and fill its key buffer as bytes
/// </summary>
constructor Create(Const AKeys: TByteDynArray); overload;
/// <summary>
/// Create an instance of TOlfCryptDecrypt class and fill its key buffer as integers
/// </summary>
constructor Create(Const AKeys: TIntegerDynArray); overload;
/// <summary>
/// DEPRECATED - internally use XORCrypt
/// </summary>
function Crypt(Const AStream: TStream): TStream; overload;
deprecated 'Use XORCrypt()';
/// <summary>
/// DEPRECATED - internally use XORCrypt
/// </summary>
class function Crypt(Const AStream: TStream; Const AKeys: TByteDynArray)
: TStream; overload; deprecated 'Use XORCrypt()';
/// <summary>
/// DEPRECATED - internally use XORDecrypt
/// </summary>
function Decrypt(Const AStream: TStream): TStream; overload;
deprecated 'Use XORDecrypt()';
/// <summary>
/// DEPRECATED - internally use XORDecrypt
/// </summary>
class function Decrypt(Const AStream: TStream; Const AKeys: TByteDynArray)
: TStream; overload; deprecated 'Use XORDecrypt()';
end;
implementation
uses
System.Generics.Collections,
System.SysUtils;
{ TOlfCryptDecrypt }
constructor TOlfCryptDecrypt.Create(const AKeys: TByteDynArray);
var
i: uint64;
begin
Create;
setlength(FByteKeys, length(AKeys));
for i := 0 to length(AKeys) - 1 do
FByteKeys[i] := AKeys[i];
end;
function TOlfCryptDecrypt.Crypt(const AStream: TStream): TStream;
begin
result := XORCrypt(AStream);
end;
constructor TOlfCryptDecrypt.Create(const AKeys: TIntegerDynArray);
var
i: uint64;
begin
Create;
setlength(FIntegerKeys, length(AKeys));
for i := 0 to length(AKeys) - 1 do
FIntegerKeys[i] := AKeys[i];
end;
class function TOlfCryptDecrypt.Crypt(const AStream: TStream;
const AKeys: TByteDynArray): TStream;
begin
result := XORCrypt(AStream, AKeys);
end;
function TOlfCryptDecrypt.Decrypt(const AStream: TStream): TStream;
begin
result := XORDecrypt(AStream)
end;
class function TOlfCryptDecrypt.Decrypt(const AStream: TStream;
const AKeys: TByteDynArray): TStream;
begin
result := XORDecrypt(AStream, AKeys);
end;
class function TOlfCryptDecrypt.GenIDBKey(const Size: word): TIntegerDynArray;
var
i: word;
begin
if Size < 1 then
raise exception.Create('The size must be greater than 0.');
setlength(result, Size);
for i := 0 to Size - 1 do
repeat
result[i] := random(255 + 255 + 1) - 255;
until result[i] <> 0;
end;
class function TOlfCryptDecrypt.GenShiftKey(const Size: word): TIntegerDynArray;
var
i: word;
begin
if Size < 1 then
raise exception.Create('The size must be greater than 0.');
setlength(result, Size);
for i := 0 to Size - 1 do
repeat
result[i] := random(7 + 7 + 1) - 7;
until result[i] <> 0;
end;
class function TOlfCryptDecrypt.GenSwapKey: TByteDynArray;
var
i: integer;
nb: integer;
Bytes: TList<byte>;
begin
setlength(result, 256);
Bytes := TList<byte>.Create;
try
for i := 0 to 255 do
Bytes.add(i);
for i := 0 to 255 do
begin
nb := random(Bytes.count);
result[i] := Bytes[nb];
Bytes.Delete(nb);
end;
finally
Bytes.free;
end;
end;
class function TOlfCryptDecrypt.GenXORKey(const Size: word): TByteDynArray;
var
i: word;
begin
if Size < 1 then
raise exception.Create('The size must be greater than 0.');
setlength(result, Size);
for i := 0 to Size - 1 do
begin
result[i] := random(256);
if (i = 0) then
while (result[0] in [0, 255]) do
result[0] := random(255);
end;
end;
class function TOlfCryptDecrypt.IDBCrypt(const AStream: TStream;
const AKeys: TIntegerDynArray): TMemoryStream;
var
KeyIndex: uint64;
KeyLength: uint64;
oc, od: byte;
IncDecValue: integer;
begin
KeyLength := length(AKeys);
if (KeyLength = 0) then
raise exception.Create('Need a private key to crypt !');
if not assigned(AStream) then
result := nil
else
begin
result := TMemoryStream.Create;
KeyIndex := 0;
AStream.position := 0;
while (AStream.position < AStream.Size) do
begin
if (1 <> AStream.Read(od, 1)) then
raise exception.Create('Can''t read a new byte.');
IncDecValue := AKeys[KeyIndex] + od;
while IncDecValue > 255 do
dec(IncDecValue, 255);
while IncDecValue < 0 do
inc(IncDecValue, 255);
oc := IncDecValue;
if (1 <> result.write(oc, 1)) then
raise exception.Create('Can''t write encrypted byte.');
if (KeyIndex + 1 < KeyLength) then
inc(KeyIndex)
else
KeyIndex := 0;
end;
end;
end;
function TOlfCryptDecrypt.IDBCrypt(const AStream: TStream): TMemoryStream;
begin
result := IDBCrypt(AStream, FIntegerKeys);
end;
function TOlfCryptDecrypt.IDBDecrypt(const AStream: TStream): TMemoryStream;
begin
result := IDBDecrypt(AStream, FIntegerKeys);
end;
class function TOlfCryptDecrypt.IDBDecrypt(const AStream: TStream;
const AKeys: TIntegerDynArray): TMemoryStream;
var
KeyIndex: uint64;
KeyLength: uint64;
oc, od: byte;
IncDecValue: integer;
begin
KeyLength := length(AKeys);
if (KeyLength = 0) then
raise exception.Create('Need a private key to crypt !');
if not assigned(AStream) then
result := nil
else
begin
result := TMemoryStream.Create;
KeyIndex := 0;
AStream.position := 0;
while (AStream.position < AStream.Size) do
begin
if (1 <> AStream.Read(od, 1)) then
raise exception.Create('Can''t read a new byte.');
IncDecValue := od - AKeys[KeyIndex];
while IncDecValue > 255 do
dec(IncDecValue, 255);
while IncDecValue < 0 do
inc(IncDecValue, 255);
oc := IncDecValue;
if (1 <> result.write(oc, 1)) then
raise exception.Create('Can''t write encrypted byte.');
if (KeyIndex + 1 < KeyLength) then
inc(KeyIndex)
else
KeyIndex := 0;
end;
end;
end;
class function TOlfCryptDecrypt.XORCrypt(const AStream: TStream;
const AKeys: TByteDynArray): TMemoryStream;
var
Key1, Key2: byte;
KeyIndex: uint64;
KeyLength: uint64;
oc, od: byte;
begin
KeyLength := length(AKeys);
if (KeyLength = 0) then
raise exception.Create('Need a private key to crypt !');
if not assigned(AStream) then
result := nil
else
begin
result := TMemoryStream.Create;
Key1 := 0;
KeyIndex := 0;
Key2 := AKeys[KeyIndex];
AStream.position := 0;
while (AStream.position < AStream.Size) do
begin
if (1 <> AStream.Read(od, 1)) then
raise exception.Create('Can''t read a new byte.');
oc := (od xor Key1) xor Key2;
if (1 <> result.write(oc, 1)) then
raise exception.Create('Can''t write encrypted byte.');
if (KeyIndex + 1 < KeyLength) then
inc(KeyIndex)
else
KeyIndex := 0;
Key1 := od;
Key2 := AKeys[KeyIndex];
end;
end;
end;
constructor TOlfCryptDecrypt.Create;
begin
inherited;
setlength(FIntegerKeys, 0);
setlength(FByteKeys, 0);
end;
function TOlfCryptDecrypt.XORCrypt(const AStream: TStream): TMemoryStream;
begin
result := XORCrypt(AStream, FByteKeys);
end;
function TOlfCryptDecrypt.XORDecrypt(const AStream: TStream): TMemoryStream;
begin
result := XORDecrypt(AStream, FByteKeys);
end;
procedure TOlfCryptDecrypt.SetByteKeys(const Value: TByteDynArray);
begin
FByteKeys := Value;
end;
procedure TOlfCryptDecrypt.SetIntegerKeys(const Value: TIntegerDynArray);
begin
FIntegerKeys := Value;
end;
class function TOlfCryptDecrypt.ShiftCrypt(const AStream: TStream;
const AKeys: TIntegerDynArray): TMemoryStream;
var
KeyIndex: uint64;
KeyLength: uint64;
oc, od: byte;
w, wo: word;
ShiftValue: byte;
begin
KeyLength := length(AKeys);
if (KeyLength = 0) then
raise exception.Create('Need a private key to crypt !');
if not assigned(AStream) then
result := nil
else
begin
result := TMemoryStream.Create;
KeyIndex := 0;
AStream.position := 0;
while (AStream.position < AStream.Size) do
begin
if (1 <> AStream.Read(od, 1)) then
raise exception.Create('Can''t read a new byte.');
ShiftValue := abs(AKeys[KeyIndex]) mod 8;
wo := od;
if AKeys[KeyIndex] = 0 then
w := od
else if AKeys[KeyIndex] > 0 then
w := wo shl ShiftValue
else
w := wo shl (8 - ShiftValue); // SHR inversé
oc := (w mod 256) + (w div 256);
if (1 <> result.write(oc, 1)) then
raise exception.Create('Can''t write encrypted byte.');
if (KeyIndex + 1 < KeyLength) then
inc(KeyIndex)
else
KeyIndex := 0;
end;
end;
end;
function TOlfCryptDecrypt.ShiftCrypt(const AStream: TStream): TMemoryStream;
begin
result := ShiftCrypt(AStream, FIntegerKeys);
end;
function TOlfCryptDecrypt.ShiftDecrypt(const AStream: TStream): TMemoryStream;
begin
result := ShiftDecrypt(AStream, FIntegerKeys);
end;
class function TOlfCryptDecrypt.ShiftDecrypt(const AStream: TStream;
const AKeys: TIntegerDynArray): TMemoryStream;
var
KeyIndex: uint64;
KeyLength: uint64;
oc, od: byte;
w, wo: word;
ShiftValue: byte;
begin
KeyLength := length(AKeys);
if (KeyLength = 0) then
raise exception.Create('Need a private key to crypt !');
if not assigned(AStream) then
result := nil
else
begin
result := TMemoryStream.Create;
KeyIndex := 0;
AStream.position := 0;
while (AStream.position < AStream.Size) do
begin
if (1 <> AStream.Read(od, 1)) then
raise exception.Create('Can''t read a new byte.');
ShiftValue := abs(AKeys[KeyIndex]) mod 8;
wo := od;
if AKeys[KeyIndex] = 0 then
w := wo
else if AKeys[KeyIndex] < 0 then
w := wo shl ShiftValue
else
w := wo shl (8 - ShiftValue); // SHR inversé
oc := (w mod 256) + (w div 256);
if (1 <> result.write(oc, 1)) then
raise exception.Create('Can''t write encrypted byte.');
if (KeyIndex + 1 < KeyLength) then
inc(KeyIndex)
else
KeyIndex := 0;
end;
end;
end;
function TOlfCryptDecrypt.SwapCrypt(const AStream: TStream): TMemoryStream;
begin
result := SwapCrypt(AStream, FByteKeys);
end;
class function TOlfCryptDecrypt.SwapCrypt(const AStream: TStream;
const AKeys: TByteDynArray): TMemoryStream;
var
oc, od: byte;
begin
if (length(AKeys) <> 256) then
raise exception.Create('Need a 256 bytes private key to crypt !');
if not assigned(AStream) then
result := nil
else
begin
result := TMemoryStream.Create;
AStream.position := 0;
while (AStream.position < AStream.Size) do
begin
if (1 <> AStream.Read(od, 1)) then
raise exception.Create('Can''t read a new byte.');
oc := AKeys[ord(od)];
if (1 <> result.write(oc, 1)) then
raise exception.Create('Can''t write encrypted byte.');
end;
end;
end;
function TOlfCryptDecrypt.SwapDecrypt(const AStream: TStream): TMemoryStream;
begin
result := SwapDecrypt(AStream, FByteKeys);
end;
class function TOlfCryptDecrypt.SwapDecrypt(const AStream: TStream;
const AKeys: TByteDynArray): TMemoryStream;
var
oc, od: byte;
ReverseKey: TByteDynArray;
i: integer;
begin
if (length(AKeys) <> 256) then
raise exception.Create('Need a 256 bytes private key to decrypt !');
if not assigned(AStream) then
result := nil
else
begin
setlength(ReverseKey, length(AKeys));
for i := 0 to length(AKeys) - 1 do
ReverseKey[AKeys[i]] := i;
result := TMemoryStream.Create;
AStream.position := 0;
while (AStream.position < AStream.Size) do
begin
if (1 <> AStream.Read(oc, 1)) then
raise exception.Create('Can''t read a new byte.');
od := ReverseKey[ord(oc)];
if (1 <> result.write(od, 1)) then
raise exception.Create('Can''t write encrypted byte.');
end;
end;
end;
class function TOlfCryptDecrypt.XORDecrypt(const AStream: TStream;
const AKeys: TByteDynArray): TMemoryStream;
var
Key1, Key2: byte;
KeyIndex: uint64;
KeyLength: uint64;
oc, od: byte;
begin
KeyLength := length(AKeys);
if (KeyLength = 0) then
raise exception.Create('Need a private key to crypt !');
if not assigned(AStream) then
result := nil
else
begin
result := TMemoryStream.Create;
Key1 := 0;
KeyIndex := 0;
Key2 := AKeys[KeyIndex];
AStream.position := 0;
while (AStream.position < AStream.Size) do
begin
if (1 <> AStream.Read(oc, 1)) then
raise exception.Create('Can''t read a new byte.');
od := (oc xor Key1) xor Key2;
if (1 <> result.write(od, 1)) then
raise exception.Create('Can''t write decrypted byte.');
if (KeyIndex + 1 < KeyLength) then
inc(KeyIndex)
else
KeyIndex := 0;
Key1 := od;
Key2 := AKeys[KeyIndex];
end;
end;
end;
initialization
randomize;
end.