I use Delphi XE and Lockbox3.5, I want to encyrpt a string having a public key provided by the payment gateway that requires the operation, the public key is something like : -----BEGIN PUBLIC KEY----- staff here -----END PUBLIC KEY----- I am not able the make RSA codec read this public key, my code is as follows:
var
Ciphertext: string;
ms: TStream;
begin
ms := TFileStream.Create('C:\PubKey.txt', fmOpenRead);
ms.Seek(0, soFromBeginning);
cdcRSA.StreamCipherId := RSA_ProgId;
cdcRSA.ChainModeId := RSA_ProgId;
Signatory1.LoadKeysFromStream(ms, [partPublic]);
cdcRSA.EncryptAnsiString('WORDSOMEWORDSOME', Ciphertext);
Memo1.Lines.Add(Ciphertext);
end;
Codec cdcRA is linked to CryptoLibrary and cipher is (RSA public key encryption system *),chaining mode is empty, but this code fails with out of memory error. Thanks for any hints..
The following code from the demo does not work neither, can someone provide an example to encrypt a string with RSA and public key? :
procedure TForm1.btnRSAClick(Sender: TObject);
var
sKey, Ciphertext: string;
ss: TStringStream;
Key: TSymetricKey;
begin
sKey := '-----BEGIN PUBLIC KEY-----' +
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlAVd8PUhIiuA00vlUZYm0xrk+' +
'UgyZxwWZSrysOJWk0POGJ91hUUqr659mBA7bg0i07Y4T+FYdA4iuvg5bT2aSMAGl' +
'To3GRKvwX8RAnimQQNqkqDk3nf20OiEygwWDQr72fWzKLtuoo7Rd5onrXEp1qM3o' +
'ywRq5Mwk4dHPX1F5EwIDAQAB' +
'-----END PUBLIC KEY-----';
ss := TStringStream.Create(sKey); ss.Seek(0,soFromBeginning);
Base64_to_stream(skey, ss);
cdcRSA.Reset;
cdcRSA.StreamCipherId := 'native.RSA';
cdcRSA.ChainModeId:= 'native.CBC';
cdcRSA.AsymetricKeySizeInBits := 1024;
key := cdcRSA.Asymetric_Engine.CreateFromStream(ss, [partPublic]); // error out of memory
cdcRSA.InitFromKey(key);
cdcRSA.EncryptString('WORDSOMEWORDSOME', Ciphertext, TEncoding.UTF8);
Memo1.Lines.Add(Ciphertext);
end;