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

Load a Private Key from a JWK

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadJwk method, which loads a private key from JWK (JSON Web Key) JSON. The only argument is the JWK JSON string. Supported key types are RSA, EC, and Ed25519 (kty=OKP).

Background: JWK is the JSON key format used throughout modern web security — JWT signing, OAuth, OpenID Connect — so this loader is what you use to consume a key from a JWK Set or an identity provider. A private JWK includes the private parameters (d and, for RSA, the CRT values); a public-only JWK omits them. As JSON in the clear, a private JWK must be handled as sensitive 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;
  jwk: string;

begin
  success := False;

  //  Demonstrates the PrivateKey.LoadJwk method, which loads a private key from JWK (JSON Web Key)
  //  JSON.  The only argument is the JWK JSON string.  Supported key types are RSA, EC, and
  //  Ed25519 (kty=OKP).

  privKey := TPrivateKey.Create;

  //  A private JWK.  For a real key, "d" (and the other private parameters) would be present.
  jwk := '{"kty":"EC","crv":"P-256","x":"...","y":"...","d":"..."}';

  success := privKey.LoadJwk(jwk);
  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.