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

RSA Import Public Key from Certificate PEM

See more RSA Examples

Uses a certificate's public key for RSA encryption. The public key from the certificate .pem file is used.

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.Rsa,
  Chilkat.PublicKey,
  Chilkat.Cert;

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

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  pubKey: TPublicKey;
  rsa: TRsa;
  encryptedStr: string;

begin
  success := False;

  cert := TCert.Create;

  success := cert.LoadFromFile('qa_data/pem/mf_public_rsa.pem');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  pubKey := TPublicKey.Create;
  cert.GetPublicKey(pubKey);

  rsa := TRsa.Create;
  rsa.UsePublicKey(pubKey);

  rsa.EncodingMode := 'base64';
  encryptedStr := rsa.EncryptStringENC('hello',False);
  WriteLn('encrypted string = ' + encryptedStr);


  cert.Free;
  pubKey.Free;
  rsa.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.