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

Load a Private Key from XML Text

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadXml method, which loads a private key from XML text. The only argument is the XML string. Supported roots include RSAKeyValue, DSAKeyValue, ECCKeyValue, and Ed25519KeyValue.

Background: XML key representations such as RSAKeyValue come from the .NET/XML-DSIG world, where keys are exchanged as XML elements. This loads that form — the inverse of GetXml — which is handy for interoperating with .NET code or systems built around XML signatures. It is a plaintext format, so protect the XML like any unencrypted key material.

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;

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

procedure RunDemo;
var
  success: Boolean;
  privKey: TPrivateKey;
  xmlText: string;

begin
  success := False;

  //  Demonstrates the PrivateKey.LoadXml method, which loads a private key from XML text.  The only
  //  argument is the XML string.  Supported roots include RSAKeyValue, DSAKeyValue, ECCKeyValue,
  //  and Ed25519KeyValue.

  privKey := TPrivateKey.Create;

  //  The XML text of a private key (for example as produced by GetXml).
  xmlText := '<RSAKeyValue><Modulus>...</Modulus><Exponent>AQAB</Exponent>...</RSAKeyValue>';

  success := privKey.LoadXml(xmlText);
  if (success = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  WriteLn('Loaded a ' + privKey.KeyType + ' private key.');


  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.