Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

RSA Encrypt and OpenSSL Decrypt

See more OpenSSL Examples

Demonstrates how to use Chilkat to RSA encrypt, and then use OpenSSL to decrypt.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.BinData,
  Chilkat.PrivateKey,
  Chilkat.Rsa,
  Chilkat.PublicKey;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  rsa: TRsa;
  privKey: TPrivateKey;
  pubKey: TPublicKey;
  plainText: string;
  bUsePrivateKey: Boolean;
  encryptedStr: string;
  bd: TBinData;

begin
  success := False;

  //  This requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  rsa := TRsa.Create;

  privKey := TPrivateKey.Create;
  success := rsa.GenKey(2048,privKey);
  success := privKey.SavePkcs8PemFile('qa_output/privKey.pem');

  pubKey := TPublicKey.Create;
  privKey.ToPublicKey(pubKey);

  rsa.EncodingMode := 'base64';
  plainText := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890';
  bUsePrivateKey := False;
  rsa.UsePublicKey(pubKey);
  encryptedStr := rsa.EncryptStringENC(plainText,bUsePrivateKey);

  bd := TBinData.Create;
  bd.AppendEncoded(encryptedStr,'base64');
  success := bd.WriteFile('qa_output/enc.dat');

  //  The OpenSSL command to decrypt is:
  //  openssl pkeyutl -in enc.dat -inkey privKey.pem -keyform PEM -pkeyopt rsa_padding_mode:pkcs1 -decrypt

  WriteLn('OK');


  rsa.Free;
  privKey.Free;
  pubKey.Free;
  bd.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.