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

Load a Public Key from a String

Demonstrates the Chilkat PublicKey.LoadFromString method, which loads a public key from a string. The only argument is the string. Chilkat examines the content and recognizes PEM public keys, unencrypted PEM private keys (taking only the public portion), XML, JWK, and more.

Background: This is the forgiving text loader: rather than requiring you to know the exact format, it inspects whatever string you provide and figures it out. A useful convenience is that it accepts an unencrypted private key too and keeps only the public half — handy when you have a key pair but need just the public key. Use it whenever the textual format is uncertain or varies by source.

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

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

procedure RunDemo;
var
  success: Boolean;
  pubKey: TPublicKey;
  keyText: string;

begin
  success := False;

  //  Demonstrates the PublicKey.LoadFromString method, which loads a public key from a string.  The
  //  only argument is the string.  Chilkat examines the content and recognizes PEM public keys,
  //  unencrypted PEM private keys (using only the public portion), XML, JWK, and more.

  pubKey := TPublicKey.Create;

  //  The PEM text of a public key.
  keyText := '-----BEGIN PUBLIC KEY-----' + #10 + 'MIIBIjANBgkq...IDAQAB' + #10 + '-----END PUBLIC KEY-----';

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

  WriteLn('Loaded a ' + pubKey.KeyType + ' public key.');


  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.