Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Generate RSA Public/Private Key Pair and Export to PEM
See more RSA Examples
_LANGUAGE_ example code showing how to generate an RSA public/private key pair and export to PEM files.Chilkat Pascal (Lazarus/Delphi) Downloads
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;
privKey: TPrivateKey;
pubKey: TPublicKey;
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 2048-bit key. Chilkat RSA supports
// key sizes ranging from 512 bits to 8192 bits.
privKey := TPrivateKey.Create;
success := rsa.GenKey(2048,privKey);
if (success = False) then
begin
WriteLn(rsa.LastErrorText);
Exit;
end;
pubKey := TPublicKey.Create;
privKey.ToPublicKey(pubKey);
// Save the private key in PEM format:
success := privKey.SavePemFile('privateKey.pem');
if (success = False) then
begin
WriteLn(privKey.LastErrorText);
Exit;
end;
// Save the public key in PEM format:
success := pubKey.SavePemFile(False,'publicKey.pem');
if (success = False) then
begin
WriteLn(pubKey.LastErrorText);
Exit;
end;
WriteLn('Success.');
rsa.Free;
privKey.Free;
pubKey.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.