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

Set the JWE Protected Header

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetProtectedHeader, which sets the integrity-protected JWE header from a JsonObject. The header specifies the key-management algorithm (alg) and content-encryption algorithm (enc).

Background. The protected header is integrity-protected and applies to all recipients. It is configured before encrypting.

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.Jwe,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  jwe: TJwe;
  header: TJsonObject;

begin
  success := False;

  jwe := TJwe.Create;

  //  Build the JWE protected header.  The "alg" member names the key-management algorithm and the "enc"
  //  member names the content-encryption algorithm.
  header := TJsonObject.Create;
  header.UpdateString('alg','A256KW');
  header.UpdateString('enc','A256GCM');

  //  Set the protected header on the JWE.  The protected header is integrity-protected and applies to
  //  all recipients.
  success := jwe.SetProtectedHeader(header);
  if (success = False) then
    begin
      WriteLn(jwe.LastErrorText);
      Exit;
    end;
  WriteLn('Protected header set.');


  jwe.Free;
  header.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.