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

Load Certificate from .cer and Private Key from .pem

See more Certificates Examples

Load a certificate from a .cer and its associated private key from a .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.Cert,
  Chilkat.StringBuilder;

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

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  sbPem: TStringBuilder;

begin
  success := False;

  cert := TCert.Create;
  success := cert.LoadFromFile('C:/certs_and_keys/Certificate.cer');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  sbPem := TStringBuilder.Create;
  success := sbPem.LoadFile('C:/certs_and_keys/PrivateKey.pem','utf-8');
  if (success = False) then
    begin
      WriteLn('Failed to load private key PEM');
      Exit;
    end;

  success := cert.SetPrivateKeyPem(sbPem.GetAsString());
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  WriteLn('The certificate and associated private key are now loaded and ready for signing.');


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