Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Jwe, Jwt, PublicKey, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
json: HCkJsonObject;
pubkey: HCkPublicKey;
jwt: HCkJwt;
jweProtHdr: HCkJsonObject;
jwe: HCkJwe;
plainText: PWideChar;
strJwe: PWideChar;

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 := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'use','enc');
CkJsonObject_UpdateString(json,'kid','puk_idp_enc');
CkJsonObject_UpdateString(json,'kty','EC');
CkJsonObject_UpdateString(json,'crv','BP-256');
CkJsonObject_UpdateString(json,'x','QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w');
CkJsonObject_UpdateString(json,'y','AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu');

pubkey := CkPublicKey_Create();

success := CkPublicKey_LoadFromString(pubkey,CkJsonObject__emit(json));
if (success = False) then
  begin
    Memo1.Lines.Add(CkPublicKey__lastErrorText(pubkey));
    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 := CkJwt_Create();

jweProtHdr := CkJsonObject_Create();
CkJsonObject_UpdateString(jweProtHdr,'alg','ECDH-ES');
CkJsonObject_UpdateString(jweProtHdr,'enc','A256GCM');
CkJsonObject_UpdateInt(jweProtHdr,'exp',CkJwt_GenNumericDate(jwt,3600));
CkJsonObject_UpdateString(jweProtHdr,'cty','NJWT');
CkJsonObject_UpdateString(jweProtHdr,'epk.kty','EC');
CkJsonObject_UpdateString(jweProtHdr,'epk.x','QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w');
CkJsonObject_UpdateString(jweProtHdr,'epk.y','AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu');
CkJsonObject_UpdateString(jweProtHdr,'epk.crv','BP-256');

jwe := CkJwe_Create();
CkJwe_SetProtectedHeader(jwe,jweProtHdr);
CkJwe_SetPublicKey(jwe,0,pubkey);

plainText := 'This is the text to be encrypted.';
strJwe := CkJwe__encrypt(jwe,plainText,'utf-8');
if (CkJwe_getLastMethodSuccess(jwe) <> True) then
  begin
    Memo1.Lines.Add(CkJwe__lastErrorText(jwe));
    Exit;
  end;

Memo1.Lines.Add(strJwe);

Memo1.Lines.Add('Success.');

CkJsonObject_Dispose(json);
CkPublicKey_Dispose(pubkey);
CkJwt_Dispose(jwt);
CkJsonObject_Dispose(jweProtHdr);
CkJwe_Dispose(jwe);

end;