Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a Delphi component/library that allows me to encrypt/decrypt some text using RSA

I need a component or library (as simple as possible, and no DLLs would be great) to encrypt a text, decrypt another, using public keys generated by OpenSSL.

I thought I'd use LockBox (the new version, v3), but according to other users here it's not as good as the old version, and more importantly, cannot use keys from other libraries. (see OpenSSL's PEM file and Lockbox3 interoperability)

I'm using Delphi 7. Any suggestions?

like image 453
TheNewbie Avatar asked Jul 31 '11 11:07

TheNewbie


1 Answers

We use Lockbox 2 in Delphi 2010 and it works great. I guess it should also work with Delphi 7. Here's a code sample:

unit LBRSA; 

interface

uses
  LbCipher,
  LbRSA,
  LbString,
  LbUtils;

  function DecryptRSA(const CipherText: String): String; overload; overload;
  function DecryptRSA(const CipherText, Exponent, Modulus: String): String; overload;

implemention


function EncryptRSA(const ClearText, Exponent, Modulus: String): String;
var
  RSA: TLbRSA;
begin
  RSA := TLbRSA.Create(nil);
  try
    RSA.PublicKey.ExponentAsString := Exponent;
    RSA.PublicKey.ModulusAsString := Modulus;

    Result := RSA.EncryptStringW(ClearText);
  finally
    FreeAndNil(RSA);
  end;
end;

function DecryptRSA(const CipherText, Exponent, Modulus: String): String;
var
  RSA: TLbRSA;
begin
  RSA := TLbRSA.Create(nil);
  try
    RSA.PrivateKey.ExponentAsString := Exponent;
    RSA.PrivateKey.ModulusAsString := Modulus;

    Result := RSA.DecryptStringW(CipherText);
  finally
    FreeAndNil(RSA);
  end;
end;

end.

Lockbox includes a demo app that lets you generate the public and private keys.

like image 109
norgepaul Avatar answered Sep 28 '22 16:09

norgepaul