-
Notifications
You must be signed in to change notification settings - Fork 4
/
AsymmetricEncryptionFrm.pas
575 lines (538 loc) · 18.1 KB
/
AsymmetricEncryptionFrm.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
{******************************************************************************}
{ }
{ WinRT Crypto Sample Applications }
{ Copyright (c) 2023 Christoph Schneider }
{ Schneider Infosystems AG, Switzerland }
{ https://github.com/SchneiderInfosystems/WinRTCrypto }
{ }
{******************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{******************************************************************************}
unit AsymmetricEncryptionFrm;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes, System.Win.WinRT,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
Winapi.CommonTypes, Winapi.WinRT,
Winapi.Security.Cryptography;
type
TfrmAsymmetricEncryption = class(TForm)
EditClear: TEdit;
btnEncrypt: TButton;
EditEncrypted: TEdit;
btnDecrypt: TButton;
cboAlgo: TComboBox;
LabelAlgo: TLabel;
EditResult: TEdit;
ShapeResult: TShape;
btnCreateKeys: TButton;
LabelResult: TLabel;
EditPublicKey: TEdit;
LabelPublicKey: TLabel;
EditPrivateKey: TEdit;
LabelPrivateKey: TLabel;
btnSaveKeys: TButton;
btnSaveEncrypt: TButton;
btnLoadEncrypt: TButton;
btnLoadKeys: TButton;
btnLoadPubKey: TButton;
cboKeySize: TComboBox;
LabelKeySize: TLabel;
lblChiffreLen: TLabel;
Label1: TLabel;
lblResult: TLabel;
lblResultingClearText: TLabel;
btnSign: TButton;
btnVerify: TButton;
btnSaveSign: TButton;
btnLoadSign: TButton;
procedure btnEncryptClick(Sender: TObject);
procedure btnDecryptClick(Sender: TObject);
procedure btnCreateKeysClick(Sender: TObject);
procedure btnSaveKeysClick(Sender: TObject);
procedure btnSaveEncryptClick(Sender: TObject);
procedure btnLoadEncryptClick(Sender: TObject);
procedure btnLoadKeysClick(Sender: TObject);
procedure btnLoadPubKeyClick(Sender: TObject);
procedure cboAlgoChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure EditKeyChange(Sender: TObject);
procedure EditEncryptedChange(Sender: TObject);
procedure btnSignClick(Sender: TObject);
procedure btnVerifyClick(Sender: TObject);
procedure btnSaveSignClick(Sender: TObject);
procedure btnLoadSignClick(Sender: TObject);
private
fDataFolder: string;
function SelectedAlgo: Core_IAsymmetricKeyAlgorithmProvider;
function SelectedAsymSignAlgo: Core_IAsymmetricKeyAlgorithmProvider;
function SelectedKeyLength: integer;
procedure InitWithNewKeys;
procedure CheckButtons;
procedure ShowError(const Msg: string);
procedure ShowWarning(const Msg: string);
procedure ShowResult(const Msg: string);
procedure ClearResult;
end;
var
frmAsymmetricEncryption: TfrmAsymmetricEncryption;
implementation
uses
Winapi.Security.Helpers;
{$R *.dfm}
resourcestring
rsTS = 'Test sample for asymmetrical encryption %s at %s with Unicode Ж•₱₲₳‡₾';
{$REGION 'GUI Handling'}
procedure TfrmAsymmetricEncryption.FormCreate(Sender: TObject);
begin
cboAlgoChange(Sender);
end;
procedure TfrmAsymmetricEncryption.cboAlgoChange(Sender: TObject);
begin
fDataFolder :=
IncludeTrailingPathDelimiter(
ExpandFileName(
ExtractFileDir(Application.ExeName) + '\..\..\Data\' + cboAlgo.Text));
if not DirectoryExists(fDataFolder, false) then
ForceDirectories(fDataFolder);
EditPublicKey.Text := '';
EditPrivateKey.Text := '';
btnEncrypt.Enabled := false;
btnDecrypt.Enabled := false;
btnSaveKeys.Enabled := false;
EditClear.Text := Format(rsTS, [cboAlgo.Text, TimeToStr(Now)]);
btnLoadKeys.Enabled := FileExists(fDataFolder + 'PrivateAndPublic.key');
btnLoadPubKey.Enabled := FileExists(fDataFolder + 'Public.key');
btnLoadEncrypt.Enabled := FileExists(fDataFolder + 'Encrypted.txt');
btnLoadSign.Enabled := FileExists(fDataFolder + 'Signature.txt');
if cboAlgo.ItemIndex = 5 then
begin
cboKeySize.Items.CommaText := '256';
cboKeySize.ItemIndex := 0;
end
else if cboKeySize.Items.CommaText = '256' then
begin
cboKeySize.Items.CommaText := '2048,3072,4096';
cboKeySize.ItemIndex := 0;
end;
cboKeySize.Enabled := cboAlgo.ItemIndex < 5;
end;
procedure TfrmAsymmetricEncryption.InitWithNewKeys;
begin
btnEncrypt.Enabled := (length(EditPublicKey.Text) > 0) and
(cboAlgo.ItemIndex < 5);
btnLoadEncrypt.Enabled := true;
btnSign.Enabled := length(EditPrivateKey.Text) > 0;
btnDecrypt.Enabled := false;
btnVerify.Enabled := false;
btnSaveKeys.Enabled := true;
EditEncrypted.Text := '';
EditResult.Text := '';
ClearResult;
end;
{$ENDREGION}
{$REGION 'Result Panel'}
procedure TfrmAsymmetricEncryption.ShowError(const Msg: string);
begin
ShapeResult.Brush.Color := clRed;
LabelResult.Caption := Msg;
end;
procedure TfrmAsymmetricEncryption.ShowWarning(const Msg: string);
begin
ShapeResult.Brush.Color := clYellow;
LabelResult.Caption := Msg;
end;
procedure TfrmAsymmetricEncryption.ShowResult(const Msg: string);
begin
ShapeResult.Brush.Color := clLime;
LabelResult.Caption := Msg;
end;
procedure TfrmAsymmetricEncryption.ClearResult;
begin
ShapeResult.Brush.Color := clBtnFace;
LabelResult.Caption := '';
end;
procedure TfrmAsymmetricEncryption.EditEncryptedChange(Sender: TObject);
begin
CheckButtons;
end;
procedure TfrmAsymmetricEncryption.EditKeyChange(Sender: TObject);
begin
TEdit(Sender).Color := clWindow;
CheckButtons;
end;
procedure TfrmAsymmetricEncryption.CheckButtons;
begin
btnDecrypt.Enabled := (length(EditPrivateKey.Text) > 0) and
(length(EditEncrypted.Text) > 0);
end;
{$ENDREGION}
{$REGION 'Key Management'}
procedure TfrmAsymmetricEncryption.btnCreateKeysClick(Sender: TObject);
var
Keys: Core_ICryptographicKey;
PublicKey, PrivateKey: IBuffer;
begin
Screen.Cursor := crHourGlass;
try
Keys := SelectedAlgo.CreateKeyPair(SelectedKeyLength);
PublicKey := Keys.ExportPublicKey;
EditPublicKey.Text := TWinRTCryptoHelpers.EncodeAsBase64(PublicKey);
PrivateKey :=
Keys.Export(Core_CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo);
EditPrivateKey.Text := TWinRTCryptoHelpers.EncodeAsBase64(PrivateKey);
EditPublicKey.Color := clAqua;
EditPrivateKey.Color := clAqua;
InitWithNewKeys;
finally
Keys := nil;
Screen.Cursor := crDefault;
end;
end;
procedure TfrmAsymmetricEncryption.btnSaveKeysClick(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Text := EditPublicKey.Text;
sl.SaveToFile(fDataFolder + 'Public.key');
sl.Text := EditPrivateKey.Text ;
sl.SaveToFile(fDataFolder + 'PrivateAndPublic.key');
finally
sl.Free;
end;
end;
procedure TfrmAsymmetricEncryption.btnLoadKeysClick(Sender: TObject);
var
sl: TStringList;
PersonalKey: Core_ICryptographicKey;
Keys, PublicKeyAsStr: string;
PublicKey, PrivateKey: IBuffer;
NewKeys: boolean;
begin
sl := TStringList.Create;
try
sl.LoadFromFile(fDataFolder + 'PrivateAndPublic.key');
Keys := sl.Text;
finally
sl.Free;
end;
PersonalKey := nil;
try
PersonalKey := SelectedAlgo.ImportKeyPair(
TWinRTCryptoHelpers.DecodeFromBase64(Keys),
Core_CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo);
except
on e: exception do
ShowError('Import private key failed: ' + e.Message);
end;
if assigned(PersonalKey) then
begin
PublicKey := PersonalKey.ExportPublicKey;
PublicKeyAsStr := TWinRTCryptoHelpers.EncodeAsBase64(PublicKey);
NewKeys := not SameText(EditPublicKey.Text, PublicKeyAsStr);
if NewKeys then
EditPublicKey.Text := PublicKeyAsStr;
PrivateKey := PersonalKey.Export(
Core_CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo);
EditPrivateKey.Text := TWinRTCryptoHelpers.EncodeAsBase64(PrivateKey);
EditPublicKey.Color := clAqua;
EditPrivateKey.Color := clAqua;
if NewKeys then
InitWithNewKeys
else begin
btnDecrypt.Enabled := (length(EditPrivateKey.Text) > 0) and
(length(EditEncrypted.Text) > 0);
btnVerify.Enabled := (length(EditPublicKey.Text) > 0) and
(length(EditEncrypted.Text) > 0);
end;
cboKeySize.ItemIndex :=
cboKeySize.Items.IndexOf(PersonalKey.KeySize.ToString);
end;
end;
procedure TfrmAsymmetricEncryption.btnLoadPubKeyClick(Sender: TObject);
var
sl: TStringList;
PubKey: string;
PublicKey: IBuffer;
PersonalKey: Core_ICryptographicKey;
begin
sl := TStringList.Create;
try
sl.LoadFromFile(fDataFolder + 'Public.key');
PubKey := sl.Text;
finally
sl.Free;
end;
PersonalKey := nil;
try
PersonalKey := SelectedAlgo.ImportPublicKey(
TWinRTCryptoHelpers.DecodeFromBase64(PubKey));
except
on e: exception do
ShowError('Import public key failed: ' + e.Message);
end;
if assigned(PersonalKey) then
begin
PublicKey := PersonalKey.ExportPublicKey;
EditPublicKey.Text := TWinRTCryptoHelpers.EncodeAsBase64(PublicKey);
EditPrivateKey.Text := '';
EditPublicKey.Color := clAqua;
EditPrivateKey.Color := clWindow;
cboKeySize.ItemIndex :=
cboKeySize.Items.IndexOf(PersonalKey.KeySize.ToString);
InitWithNewKeys;
end;
end;
{$ENDREGION}
{$REGION 'Asymmetric Encrypt/Decryption'}
procedure TfrmAsymmetricEncryption.btnEncryptClick(Sender: TObject);
var
cleardata, encrypted: IBuffer;
PersonalKey: Core_ICryptographicKey;
begin
try
PersonalKey := SelectedAlgo.ImportPublicKey(
TWinRTCryptoHelpers.DecodeFromBase64(EditPublicKey.Text));
EditPublicKey.Color := clAqua;
EditPrivateKey.Color := clWindow;
except
on e: exception do
ShowError('Import public key failed: ' + e.Message);
end;
ClearResult;
clearData := TWinRTCryptoHelpers.StrToIBuffer(EditClear.Text);
try
encrypted := TCore_CryptographicEngine.Encrypt(PersonalKey, cleardata, nil);
EditEncrypted.Text := TWinRTCryptoHelpers.EncodeAsBase64(encrypted);
lblChiffreLen.Caption := Format('Size %d bytes', [encrypted.Length]);
btnSaveEncrypt.Enabled := true;
btnDecrypt.Enabled := length(EditPrivateKey.Text) > 0;
btnVerify.Enabled := false;
lblResult.Caption := 'Chiffre';
EditResult.Text := '';
EditResult.visible := true;
lblResultingClearText.visible := true;
except
on e: exception do
ShowError('Encryption failed: ' + e.Message);
end;
end;
procedure TfrmAsymmetricEncryption.btnDecryptClick(Sender: TObject);
var
cleardata, encrypted: IBuffer;
PersonalKey: Core_ICryptographicKey;
begin
Assert(lblResult.Caption = 'Chiffre', 'Decryption needs chiffre');
try
PersonalKey := SelectedAlgo.ImportKeyPair(
TWinRTCryptoHelpers.DecodeFromBase64(EditPrivateKey.Text),
Core_CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo);
EditPublicKey.Color := clWindow;
EditPrivateKey.Color := clAqua;
except
on e: exception do
ShowError('Import private key failed: ' + e.Message);
end;
encrypted := TWinRTCryptoHelpers.DecodeFromBase64(EditEncrypted.Text);
try
cleardata := TCore_CryptographicEngine.Decrypt(PersonalKey, encrypted, nil);
EditResult.Text := TWinRTCryptoHelpers.IBufferToStr(cleardata);
if SameText(EditClear.Text, EditResult.Text) then
ShowResult(
'Passed: Resulting clear text is identical to starting clear text')
else
ShowWarning(
'Failed: Resulting clear text different than starting clear text');
EditResult.visible := true;
lblResultingClearText.visible := true;
except
on e: exception do
ShowError('Decryption failed: ' + e.Message);
end;
end;
function TfrmAsymmetricEncryption.SelectedAlgo:
Core_IAsymmetricKeyAlgorithmProvider;
var
Algo: HString;
begin
case cboAlgo.ItemIndex of
0: Algo := TCore_AsymmetricAlgorithmNames.RsaPkcs1;
1: Algo := TCore_AsymmetricAlgorithmNames.RsaOaepSha1;
2: Algo := TCore_AsymmetricAlgorithmNames.RsaOaepSha256;
3: Algo := TCore_AsymmetricAlgorithmNames.RsaOaepSha384;
4: Algo := TCore_AsymmetricAlgorithmNames.RsaOaepSha512;
5: Algo := TCore_AsymmetricAlgorithmNames.EcdsaP256Sha256;
else
raise Exception.Create('Unknown Core_AsymmetricAlgorithmNames');
end;
result := TCore_AsymmetricKeyAlgorithmProvider.OpenAlgorithm(Algo);
cboAlgo.Enabled := false;
end;
function TfrmAsymmetricEncryption.SelectedKeyLength: integer;
begin
result := StrToInt(cboKeySize.Text);
cboKeySize.Enabled := false;
end;
{$ENDREGION}
{$REGION 'Asymmetric Signing/Verifing'}
procedure TfrmAsymmetricEncryption.btnSignClick(Sender: TObject);
var
cleardata, signature: IBuffer;
PrivateKey: Core_ICryptographicKey;
begin
try
PrivateKey := SelectedAsymSignAlgo.ImportKeyPair(
TWinRTCryptoHelpers.DecodeFromBase64(EditPrivateKey.Text),
Core_CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo);
EditPublicKey.Color := clWindow;
EditPrivateKey.Color := clAqua;
except
on e: exception do
ShowError('Import private key failed: ' + e.Message);
end;
ClearResult;
clearData := TWinRTCryptoHelpers.StrToIBuffer(EditClear.Text);
try
signature := TCore_CryptographicEngine.Sign(PrivateKey, cleardata);
EditEncrypted.Text := TWinRTCryptoHelpers.EncodeAsBase64(signature);
lblChiffreLen.Caption := Format('Size %d bytes', [signature.Length]);
btnSaveSign.Enabled := true;
btnDecrypt.Enabled := false;
btnVerify.Enabled := length(EditPublicKey.Text) > 0;
lblResult.Caption := 'Signature';
EditResult.visible := false;
lblResultingClearText.visible := false;
except
on e: exception do
ShowError('Encryption failed: ' + e.Message);
end;
end;
procedure TfrmAsymmetricEncryption.btnVerifyClick(Sender: TObject);
var
cleardata, signature: IBuffer;
PublicKey: Core_ICryptographicKey;
begin
Assert(lblResult.Caption = 'Signature', 'Verification needs signature');
try
PublicKey := SelectedAsymSignAlgo.ImportPublicKey(
TWinRTCryptoHelpers.DecodeFromBase64(EditPublicKey.Text));
EditPublicKey.Color := clAqua;
EditPrivateKey.Color := clWindow;
except
on e: exception do
ShowError('Import public key failed: ' + e.Message);
end;
clearData := TWinRTCryptoHelpers.StrToIBuffer(EditClear.Text);
signature := TWinRTCryptoHelpers.DecodeFromBase64(EditEncrypted.Text);
try
if TCore_CryptographicEngine.VerifySignature(PublicKey, clearData,
signature) then
ShowResult('Passed: Signature is valid')
else
ShowWarning('Failed: Signatur is invalid');
except
on e: exception do
ShowError('Verify signature failed: ' + e.Message);
end;
EditResult.visible := false;
lblResultingClearText.visible := false;
end;
function TfrmAsymmetricEncryption.SelectedAsymSignAlgo:
Core_IAsymmetricKeyAlgorithmProvider;
var
Algo: HString;
begin
case cboAlgo.ItemIndex of
0,
1: Algo := TCore_AsymmetricAlgorithmNames.RsaSignPkcs1Sha1;
2: Algo := TCore_AsymmetricAlgorithmNames.RsaSignPkcs1Sha256;
3: Algo := TCore_AsymmetricAlgorithmNames.RsaSignPkcs1Sha384;
4: Algo := TCore_AsymmetricAlgorithmNames.RsaSignPkcs1Sha512;
5: Algo := TCore_AsymmetricAlgorithmNames.EcdsaSha256;
6: Algo := TCore_AsymmetricAlgorithmNames.EcdsaP256Sha256;
else
raise Exception.Create('Unknown Core_AsymmetricAlgorithmNames');
end;
result := TCore_AsymmetricKeyAlgorithmProvider.OpenAlgorithm(Algo);
cboAlgo.Enabled := false;
end;
{$ENDREGION}
{$REGION 'Encrypted Message Load/Save'}
procedure TfrmAsymmetricEncryption.btnSaveEncryptClick(Sender: TObject);
var
sl: TStringList;
begin
Assert(lblResult.Caption = 'Chiffre', 'Decryption needs chiffre');
sl := TStringList.Create;
try
sl.Text := EditEncrypted.Text;
sl.SaveToFile(fDataFolder + 'Encrypted.txt');
finally
sl.Free;
end;
btnSaveEncrypt.Enabled := false;
end;
procedure TfrmAsymmetricEncryption.btnLoadEncryptClick(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.LoadFromFile(fDataFolder + 'Encrypted.txt');
EditEncrypted.Text := sl.Text;
lblResult.Caption := 'Chiffre';
btnDecrypt.Enabled := length(EditPrivateKey.Text) > 0;
btnVerify.Enabled := false;
finally
sl.Free;
end;
end;
procedure TfrmAsymmetricEncryption.btnSaveSignClick(Sender: TObject);
var
sl: TStringList;
begin
Assert(lblResult.Caption = 'Signature', 'Verification needs signature');
sl := TStringList.Create;
try
sl.Add(EditClear.Text);
sl.Add(EditEncrypted.Text);
sl.SaveToFile(fDataFolder + 'Signature.txt', TEncoding.UTF8);
finally
sl.Free;
end;
btnSaveSign.enabled := false;
end;
procedure TfrmAsymmetricEncryption.btnLoadSignClick(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.LoadFromFile(fDataFolder + 'Signature.txt', TEncoding.UTF8);
Assert(sl.Count = 2, 'Invalid signature file. Expected are 2 lines with clear text and signature.');
EditClear.Text := sl[0];
EditEncrypted.Text := sl[1];
lblResult.Caption := 'Signature';
btnDecrypt.Enabled := false;
btnVerify.Enabled := length(EditPublicKey.Text) > 0;
finally
sl.Free;
end;
end;
{$ENDREGION}
end.