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

JWE using ECDH-ES, BP-256, A256GCM

See more JSON Web Encryption (JWE) Examples

Create a JWE with the following header:
	{
	  "alg": "ECDH-ES",
	  "enc": "A256GCM",
	  "exp": 1621957030,
	  "cty": "NJWT",
	  "epk": {
	    "kty": "EC",
	    "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"
	    "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
	    "crv": "BP-256"
	  }
	}

Note: This example requires Chilkat v9.5.0.87 or greater.

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

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

procedure RunDemo;
var
  success: Boolean;
  json: TJsonObject;
  pubkey: TPublicKey;
  jwt: TJwt;
  jweProtHdr: TJsonObject;
  jwe: TJwe;
  plainText: string;
  strJwe: string;

begin
  success := False;

  //  This requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  //  Load our brainpool BP-256 public key.

  //  	{
  //  	  "use": "enc",
  //  	  "kid": "puk_idp_enc",
  //  	  "kty": "EC",
  //  	  "crv": "BP-256",
  //  	  "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w",
  //  	  "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
  //  	}

  json := TJsonObject.Create;
  json.UpdateString('use','enc');
  json.UpdateString('kid','puk_idp_enc');
  json.UpdateString('kty','EC');
  json.UpdateString('crv','BP-256');
  json.UpdateString('x','QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w');
  json.UpdateString('y','AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu');

  pubkey := TPublicKey.Create;

  success := pubkey.LoadFromString(json.Emit());
  if (success = False) then
    begin
      WriteLn(pubkey.LastErrorText);
      Exit;
    end;

  //  Build our protected header:

  //  	{
  //  	  "alg": "ECDH-ES",
  //  	  "enc": "A256GCM",
  //  	  "exp": 1621957030,
  //  	  "cty": "NJWT",
  //  	  "epk": {
  //  	    "kty": "EC",
  //  	    "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"
  //  	    "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
  //  	    "crv": "BP-256"
  //  	  }
  //  	}

  //  Use jwt only for getting the current date/time + 3600 seconds.
  jwt := TJwt.Create;

  jweProtHdr := TJsonObject.Create;
  jweProtHdr.UpdateString('alg','ECDH-ES');
  jweProtHdr.UpdateString('enc','A256GCM');
  jweProtHdr.UpdateInt('exp',jwt.GenNumericDate(3600));
  jweProtHdr.UpdateString('cty','NJWT');
  jweProtHdr.UpdateString('epk.kty','EC');
  jweProtHdr.UpdateString('epk.x','QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w');
  jweProtHdr.UpdateString('epk.y','AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu');
  jweProtHdr.UpdateString('epk.crv','BP-256');

  jwe := TJwe.Create;
  jwe.SetProtectedHeader(jweProtHdr);
  jwe.SetPublicKey(0,pubkey);

  plainText := 'This is the text to be encrypted.';
  strJwe := jwe.Encrypt(plainText,'utf-8');
  if (jwe.LastMethodSuccess <> True) then
    begin
      WriteLn(jwe.LastErrorText);
      Exit;
    end;

  WriteLn(strJwe);

  WriteLn('Success.');


  json.Free;
  pubkey.Free;
  jwt.Free;
  jweProtHdr.Free;
  jwe.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.