-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathRsaExample.pas
More file actions
177 lines (154 loc) · 6.57 KB
/
Copy pathRsaExample.pas
File metadata and controls
177 lines (154 loc) · 6.57 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
{ *********************************************************************************** }
{ * CryptoLib Library * }
{ * Author - Ugochukwu Mmaduekwe * }
{ * Github Repository <https://github.com/Xor-el> * }
{ * * }
{ * Distributed under the MIT software license, see the accompanying file LICENSE * }
{ * or visit http://www.opensource.org/licenses/mit-license.php. * }
{ * * }
{ * Acknowledgements: * }
{ * * }
{ * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
{ * the development of this library * }
{ * ******************************************************************************* * }
(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
unit RsaExample;
interface
{$IFDEF FPC}
{$MODE DELPHI}
{$HINTS OFF}
{$WARNINGS OFF}
{$ENDIF FPC}
uses
SysUtils,
Classes,
ClpConverters,
ClpIAsymmetricCipherKeyPair,
ClpIBufferedCipher,
ClpCipherUtilities,
ClpArrayUtilities,
ClpEncoders,
ExampleBase,
AsymmetricExampleUtilities,
HybridEncryption;
type
TRsaExample = class(TExampleBase)
private
procedure DoRsaEncryptDecrypt(const ACipherAlgorithm: string;
const AKeyPair: IAsymmetricCipherKeyPair);
procedure DoRsaHybridRoundtrip(const AKeyPair: IAsymmetricCipherKeyPair;
const APlainText, AAadContext: string);
procedure DoRsaHybridStreamRoundtrip(const AKeyPair: IAsymmetricCipherKeyPair;
const APlainText, AAadContext: string);
procedure RunRsaEncryptDecrypt;
procedure RunRsaDemos;
public
procedure Run; override;
end;
implementation
procedure TRsaExample.DoRsaEncryptDecrypt(const ACipherAlgorithm: string;
const AKeyPair: IAsymmetricCipherKeyPair);
var
LCipher: IBufferedCipher;
LPlain, LCipherText, LDecrypted: TBytes;
begin
Logger.LogInformation('Cipher: {0}', [ACipherAlgorithm]);
LCipher := TCipherUtilities.GetCipher(ACipherAlgorithm);
if LCipher = nil then
begin
Logger.LogWarning('Cipher "{0}" not available.', [ACipherAlgorithm]);
Exit;
end;
LPlain := TConverters.ConvertStringToBytes('Hello RSA encryption!', TEncoding.UTF8);
LCipher.Init(True, AKeyPair.Public);
LCipherText := LCipher.DoFinal(LPlain);
Logger.LogInformation('Encrypted ({0} bytes):{1}{2}', [IntToStr(System.Length(LCipherText)), sLineBreak, THexEncoder.Encode(LCipherText, False)]);
LCipher.Init(False, AKeyPair.Private);
LDecrypted := LCipher.DoFinal(LCipherText);
if TArrayUtilities.AreEqual(LPlain, LDecrypted) then
Logger.LogInformation('Decrypt roundtrip: success.', [])
else
Logger.LogError('Decrypt roundtrip: failed.', []);
end;
procedure TRsaExample.DoRsaHybridRoundtrip(const AKeyPair: IAsymmetricCipherKeyPair;
const APlainText, AAadContext: string);
var
LPlain, LAad, LEnvelope, LDecrypted: TBytes;
begin
LPlain := TConverters.ConvertStringToBytes(APlainText, TEncoding.UTF8);
LAad := TConverters.ConvertStringToBytes(AAadContext, TEncoding.UTF8);
LEnvelope := TRsaHybridEncryption.Encrypt(AKeyPair.Public, LPlain, LAad);
Logger.LogInformation('RSA hybrid envelope: {0} bytes', [IntToStr(System.Length(LEnvelope))]);
LDecrypted := TRsaHybridEncryption.Decrypt(AKeyPair.Private, LEnvelope, LAad);
if TArrayUtilities.AreEqual(LPlain, LDecrypted) then
Logger.LogInformation('RSA hybrid encrypt/decrypt roundtrip: success.', [])
else
Logger.LogError('RSA hybrid encrypt/decrypt roundtrip: failed.', []);
end;
procedure TRsaExample.DoRsaHybridStreamRoundtrip(const AKeyPair: IAsymmetricCipherKeyPair;
const APlainText, AAadContext: string);
var
LPlainStream, LEncStream, LDecStream: TBytesStream;
LPlain, LAad, LDecrypted: TBytes;
begin
LPlain := TConverters.ConvertStringToBytes(APlainText, TEncoding.UTF8);
LAad := TConverters.ConvertStringToBytes(AAadContext, TEncoding.UTF8);
LPlainStream := TBytesStream.Create(LPlain);
try
LEncStream := TBytesStream.Create(nil);
try
TRsaHybridEncryption.Encrypt(AKeyPair.Public, LPlainStream, LEncStream, LAad);
Logger.LogInformation('RSA hybrid stream envelope: {0} bytes', [IntToStr(LEncStream.Size)]);
LEncStream.Position := 0;
LDecStream := TBytesStream.Create(nil);
try
TRsaHybridEncryption.Decrypt(AKeyPair.Private, LEncStream, LDecStream, LAad);
LDecrypted := Copy(LDecStream.Bytes, 0, LDecStream.Size);
if TArrayUtilities.AreEqual(LPlain, LDecrypted) then
Logger.LogInformation('RSA hybrid stream roundtrip: success.', [])
else
Logger.LogError('RSA hybrid stream roundtrip: failed.', []);
finally
LDecStream.Free;
end;
finally
LEncStream.Free;
end;
finally
LPlainStream.Free;
end;
end;
procedure TRsaExample.RunRsaEncryptDecrypt;
var
LKp: IAsymmetricCipherKeyPair;
begin
LKp := TAsymmetricExampleUtilities.GenerateKeyPair('RSA');
DoRsaEncryptDecrypt('RSA/NONE/PKCS1PADDING', LKp);
DoRsaEncryptDecrypt('RSA/NONE/OAEPPADDING', LKp);
DoRsaEncryptDecrypt('RSA/NONE/OAEPWITHSHA-256ANDMGF1PADDING', LKp);
end;
procedure TRsaExample.RunRsaDemos;
var
LKp: IAsymmetricCipherKeyPair;
begin
LogWithLineBreak('--- RSA example: Sign and verify ---');
TAsymmetricExampleUtilities.RunSignVerify('RSA', 'SHA-256withRSA', 'Message to sign');
TAsymmetricExampleUtilities.RunSignVerify('RSA', 'SHA256WITHRSAANDMGF1', 'Message to sign');
LogWithLineBreak('--- RSA example: Key recreate from DER bytes ---');
TAsymmetricExampleUtilities.RunDerRoundtrip('RSA', 'RSA');
LogWithLineBreak('--- RSA example: PEM export/import ---');
TAsymmetricExampleUtilities.RunPemRoundtrip('RSA', 'RSA');
LogWithLineBreak('--- RSA example: Encrypt/decrypt ---');
RunRsaEncryptDecrypt;
LKp := TAsymmetricExampleUtilities.GenerateKeyPair('RSA-3072');
Logger.LogInformation('Key spec: {0}', ['RSA-3072']);
LogWithLineBreak('--- RSA example: Hybrid encrypt/decrypt (RSA-OAEP + AES-256-GCM) ---');
DoRsaHybridRoundtrip(LKp, 'Hello RSA Hybrid Encryption!', 'RH01-example-context');
LogWithLineBreak('--- RSA example: Hybrid stream encrypt/decrypt ---');
DoRsaHybridStreamRoundtrip(LKp, 'Hello RSA Hybrid Stream!', 'RH01-stream-context');
end;
procedure TRsaExample.Run;
begin
RunRsaDemos;
end;
end.