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

Generate RSA Key and Export to Encrypted PEM

See more RSA Examples

_LANGUAGE_ sample showing how to generate an RSA key and export to encrypted PEM.

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;

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

procedure RunDemo;
var
  success: Boolean;
  rsa: TRsa;
  privKey: TPrivateKey;
  strEncPem: string;

begin
  success := False;

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

  rsa := TRsa.Create;

  //  Generate a 1024-bit key.  Chilkat RSA supports
  //  key sizes ranging from 512 bits to 4096 bits.
  privKey := TPrivateKey.Create;
  success := rsa.GenKey(1024,privKey);
  if (success = False) then
    begin
      WriteLn(rsa.LastErrorText);
      Exit;
    end;

  strEncPem := privKey.GetPkcs8EncryptedPem('myPassword');

  //  You now have the private key in an encrypted format,
  //  which may be (for example) saved to a database.

  WriteLn(strEncPem);


  rsa.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.