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

Export Digital Certificate's Public Key

See more Certificates Examples

The ExportPublicKey method can be called to get a certificate's public key. It can then be saved to any of a number of formats: (1) OpenSSL DER, (2) OpenSSL PEM, (3) RSA DER, (4) XML.

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.PublicKey;

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

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  pubkey: TPublicKey;

begin
  success := False;

  cert := TCert.Create;

  //  LoadFromFile will load virtually any certificate format file.
  //  It will auto-recognize the format and load appropiately.
  success := cert.LoadFromFile('/Users/chilkat/testData/cer/chilkat.cer');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  Get the public key:
  pubkey := TPublicKey.Create;
  cert.GetPublicKey(pubkey);

  //  Save to various formats:
  success := pubkey.SaveDerFile(False,'/Users/chilkat/testData/pubkeys/chilkat_pkcs8.der');
  if (success <> True) then
    begin
      WriteLn(pubkey.LastErrorText);
      Exit;
    end;

  success := pubkey.SavePemFile(False,'/Users/chilkat/testData/pubkeys/chilkat.pem');
  if (success <> True) then
    begin
      WriteLn(pubkey.LastErrorText);
      Exit;
    end;

  success := pubkey.SaveDerFile(True,'/Users/chilkat/testData/pubkeys/chilkat_pkcs1.der');
  if (success <> True) then
    begin
      WriteLn(pubkey.LastErrorText);
      Exit;
    end;

  success := pubkey.SaveXmlFile('/Users/chilkat/testData/pubkeys/chilkat.xml');
  if (success <> True) then
    begin
      WriteLn(pubkey.LastErrorText);
      Exit;
    end;
  WriteLn('Public key exported to all file formats.');


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