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

Load PEM Public/Private Key into RSA Object

See more RSA Examples

Demonstrates how to load a PEM key into the Chilkat RSA object.

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

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

procedure RunDemo;
var
  success: Boolean;
  rsa: TRsa;
  publicKeyPem: string;
  pubkey: TPublicKey;
  privateKeyPem: string;
  privkey: TPrivateKey;

begin
  success := False;

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

  rsa := TRsa.Create;

  //  First demonstrate importing a PEM public key:
  publicKeyPem := 'PEM public-key data goes here';
  pubkey := TPublicKey.Create;

  success := pubkey.LoadFromString(publicKeyPem);
  if (success = False) then
    begin
      WriteLn(pubkey.LastErrorText);
      Exit;
    end;

  success := rsa.UsePublicKey(pubkey);
  if (success = False) then
    begin
      WriteLn(rsa.LastErrorText);
      Exit;
    end;

  //  Demonstrate importing a PEM private key:
  privateKeyPem := 'PEM private-key data goes here';
  privkey := TPrivateKey.Create;

  //  If the private key PEM is protected with a password, then 
  //  call LoadEncryptedPem.  Otherwise call LoadPem.
  success := privkey.LoadPem(privateKeyPem);
  if (success = False) then
    begin
      WriteLn(privkey.LastErrorText);
      Exit;
    end;

  success := rsa.UsePrivateKey(privkey);
  if (success = False) then
    begin
      WriteLn(rsa.LastErrorText);
      Exit;
    end;

  WriteLn('OK!');


  rsa.Free;
  pubkey.Free;
  privkey.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.