-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.SimpleEncryption.pas
More file actions
327 lines (271 loc) · 8.95 KB
/
MaxLogic.SimpleEncryption.pas
File metadata and controls
327 lines (271 loc) · 8.95 KB
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
unit MaxLogic.SimpleEncryption;
{ uses SCOP algo
this is mostly for historical backwards compatibility.
for new projects use maxASEAdapter.pas }
{
Version:3.2
History:
2017-06-18: DEC is discarded, SCOP cipher is extracted to maxLogic.Cipher.SCOP
2017-01-27: added EncryptBytes / EncryptUtf8Str / Base64Encode and their counterparts
2017-01-16: added TextFromEncryptedFile and TextToEncryptedFile
}
interface
uses
system.sysUtils, system.classes, system.hash,
MaxLogic.Cipher.SCOP;
procedure EncryptStream(InStream, OutStream: TStream; DataSize: integer; Const Password: Ansistring; Out InStreamMD5HashAsBase64: string);
procedure DecryptStream(InStream, OutStream: TStream; DataSize: integer; Const Password: Ansistring; Out OutStreamMD5HashAsBase64: string);
Function TextFromEncryptedFile(const FileName: string; const Password: Ansistring): string;
Procedure TextToEncryptedFile(const Text, FileName: string; const Password: Ansistring; encoding: TEncoding);
function EncryptBytes(const SrcBuff; size: integer; const Password: Ansistring): TBytes; overload;
function EncryptBytes(const SrcBuff: TBytes; const Password: Ansistring): TBytes; overload;
function DecryptBytes(const SrcBuff; size: integer; const Password: Ansistring): TBytes; overload;
function DecryptBytes(const SrcBuff: TBytes; const Password: Ansistring): TBytes; overload;
function EncryptUtf8Str(const s: string; const Password: Ansistring): TBytes;
Function DecryptUtf8Str(const bytes: TBytes; const Password: Ansistring): string;
// not really an encryption but helps to store binary data as string
Function Base64Encode(s: string): String; overload;
Function Base64Encode(const bytes: TBytes): string; overload;
Function Base64Encode(const Buffer; size: integer): string; overload;
Function Base64Decode(s: string): TBytes;
procedure EncryptBuffer(const SrcBuff; var OutBuff; DataSize: integer; Const Password: Ansistring);
procedure DecryptBuffer(const SrcBuff; var OutBuff; DataSize: integer; Const Password: Ansistring);
// simple helper function to get the hash as a base64 encoded string in one go
procedure EncryptBufferAndGetHash(const SrcBuff; var OutBuff; DataSize: integer; Const Password: Ansistring; Out MD5Hash: string);
procedure DecryptBufferAndGetHash(const SrcBuff; var OutBuff; DataSize: integer; Const Password: Ansistring; Out MD5Hash: string);
// as mime64 encoded
function md5(const s: string): String; overload;
function md5(const Buff; BuffSize: cardinal): string; overload;
function md5(astream: TStream; DataSize: cardinal): string; overload;
implementation
uses
system.ioUtils, system.netEncoding;
procedure DecryptBuffer(const SrcBuff; var OutBuff; DataSize: integer; Const Password: Ansistring);
var
Cipher: TScopCipher;
begin
Cipher := TScopCipher.Create(Password);
Cipher.InitKey(Password);
Cipher.DecodeBuffer(SrcBuff, OutBuff, DataSize);
Cipher.Free;
end;
procedure DecryptBufferAndGetHash(const SrcBuff; var OutBuff; DataSize: integer; Const Password: Ansistring; Out MD5Hash: string);
begin
DecryptBuffer(SrcBuff, OutBuff, DataSize, Password);
MD5Hash := md5(OutBuff, DataSize);
end;
procedure EncryptBufferAndGetHash(const SrcBuff; var OutBuff; DataSize: integer; Const Password: Ansistring; Out MD5Hash: string);
begin
MD5Hash := md5(SrcBuff, DataSize);
EncryptBuffer(SrcBuff, OutBuff, DataSize, Password);
end;
procedure DecryptStream(InStream, OutStream: TStream; DataSize: integer; Const Password: Ansistring; Out OutStreamMD5HashAsBase64: string);
var
Cipher: TScopCipher;
pos: integer;
begin
pos := OutStream.Position;
Cipher := TScopCipher.Create(Password);
Cipher.InitKey(Password);
Cipher.DecodeStream(InStream, OutStream, DataSize);
Cipher.Free;
OutStream.Position := pos;
OutStreamMD5HashAsBase64 := md5(OutStream, OutStream.size);
end;
procedure EncryptBuffer(const SrcBuff; var OutBuff; DataSize: integer; Const Password: Ansistring);
var
Cipher: TScopCipher;
begin
Cipher := TScopCipher.Create(Password);
Cipher.InitKey(Password);
Cipher.EncodeBuffer(SrcBuff, OutBuff, DataSize);
Cipher.Free;
end;
procedure EncryptStream(InStream, OutStream: TStream; DataSize: integer; Const Password: Ansistring; Out InStreamMD5HashAsBase64: string);
var
pos: integer;
Cipher: TScopCipher;
begin
pos := InStream.Position;
InStreamMD5HashAsBase64 := md5(InStream, InStream.size);
InStream.Position := pos;
Cipher := TScopCipher.Create(Password);
Cipher.InitKey(Password);
Cipher.EncodeStream(InStream, OutStream, DataSize);
Cipher.Free;
end;
function md5(const s: string): String;
var
h: system.hash.THashMD5;
begin
result :=
Base64Encode(
system.hash.THashMD5.GetHashBytes(s));
end;
function md5(const Buff; BuffSize: cardinal): string;
var
h: system.hash.THashMD5;
begin
h := system.hash.THashMD5.Create;
h.Update(Buff, BuffSize);
result :=
Base64Encode(
h.HashAsBytes);
end;
function md5(astream: TStream; DataSize: cardinal): string;
const
BUFFERSIZE = 4 * 1024;
var
LMD5: THashMD5;
LBuffer: TBytes;
LBytesRead: Longint;
BytesLeft: cardinal;
begin
LMD5 := THashMD5.Create;
SetLength(LBuffer, BUFFERSIZE);
BytesLeft := DataSize;
while BytesLeft <> 0 do
begin
if BUFFERSIZE > BytesLeft then
LBytesRead := BytesLeft
else
LBytesRead := BUFFERSIZE;
LBytesRead := astream.ReadData(LBuffer, LBytesRead);
dec(BytesLeft, LBytesRead);
if LBytesRead = 0 then
Break;
LMD5.Update(LBuffer, LBytesRead);
end;
result :=
Base64Encode(
LMD5.HashAsBytes);
end;
Function TextFromEncryptedFile(const FileName: string; const Password: Ansistring): string;
var
EncryptedStream: TMemorystream;
DeCryptedStream: TBytesStream;
MD5Hash: string;
encoding: TEncoding;
BOMLength: integer;
begin
encoding := nil;
EncryptedStream := TMemorystream.Create;
DeCryptedStream := TBytesStream.Create;
try
EncryptedStream.loadFromFile(FileName);
EncryptedStream.Position := 0;
DeCryptedStream.size := EncryptedStream.size;
DecryptStream(EncryptedStream, DeCryptedStream,
EncryptedStream.size, Password, MD5Hash);
DeCryptedStream.Position := 0;
BOMLength := TEncoding.GetBufferEncoding(DeCryptedStream.bytes, encoding);
result := encoding.GetString(DeCryptedStream.bytes, BOMLength, DeCryptedStream.size - BOMLength);
finally
EncryptedStream.Free;
DeCryptedStream.Free;
end;
end;
Procedure TextToEncryptedFile(const Text, FileName: string; const Password: Ansistring; encoding: TEncoding);
var
Buff: TBytes;
EncryptedStream, ms: TMemorystream;
MD5Hash: string;
begin
ms := TMemorystream.Create;
EncryptedStream := TMemorystream.Create;
try
if encoding = nil then
encoding := TEncoding.default;
Buff := encoding.GetPreamble;
if Length(Buff) <> 0 then
ms.WriteBuffer(Buff, Length(Buff));
Buff := encoding.GetBytes(Text);
ms.WriteBuffer(Buff, Length(Buff));
// free memory
Buff := nil;
// now encrypt
ms.Position := 0;
EncryptStream(ms, EncryptedStream, ms.size, Password, MD5Hash);
EncryptedStream.savetofile(FileName);
finally
ms.Free;
EncryptedStream.Free;
end;
end;
function EncryptBytes(const SrcBuff; size: integer; const Password: Ansistring): TBytes;
begin
SetLength(result, size);
EncryptBuffer(SrcBuff, result[0], size, Password);
end;
function EncryptBytes(const SrcBuff: TBytes; const Password: Ansistring): TBytes;
begin
result := EncryptBytes(SrcBuff[0], Length(SrcBuff), Password);
end;
function DecryptBytes(const SrcBuff; size: integer; const Password: Ansistring): TBytes;
begin
SetLength(result, size);
DecryptBuffer(SrcBuff, result[0], size, Password);
end;
function DecryptBytes(const SrcBuff: TBytes; const Password: Ansistring): TBytes;
begin
result := DecryptBytes(SrcBuff[0], Length(SrcBuff), Password);
end;
function EncryptUtf8Str(const s: string; const Password: Ansistring): TBytes;
var
c: TBytes;
begin
c := TEncoding.utf8.GetBytes(s);
result := EncryptBytes(c, Password);
end;
Function DecryptUtf8Str(const bytes: TBytes; const Password: Ansistring): string;
var
c: TBytes;
begin
c := DecryptBytes(bytes, Password);
result := TEncoding.utf8.GetString(c);
end;
Function Base64Encode(s: string): String;
var
LBase64: TNetEncoding;
begin
LBase64 := TBase64Encoding.Create(0, '');
try
result := LBase64.Encode(s);
finally
LBase64.Free;
end;
end;
Function Base64Encode(const bytes: TBytes): string;
var
LBase64: TNetEncoding;
begin
LBase64 := TBase64Encoding.Create(0, '');
try
result := LBase64.EncodeBytesToString(bytes);
finally
LBase64.Free;
end;
end;
Function Base64Encode(const Buffer; size: integer): string;
var
LBase64: TNetEncoding;
begin
LBase64 := TBase64Encoding.Create(0, '');
try
result := LBase64.EncodeBytesToString(@Buffer, size);
finally
LBase64.Free;
end;
end;
Function Base64Decode(s: string): TBytes;
var
LBase64: TNetEncoding;
begin
LBase64 := TBase64Encoding.Create(0, '');
try
result := LBase64.DecodeStringToBytes(s);
finally
LBase64.Free;
end;
end;
end.