Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

// 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"
// 	}

loJson = createobject("CkJsonObject")
loJson.UpdateString("use","enc")
loJson.UpdateString("kid","puk_idp_enc")
loJson.UpdateString("kty","EC")
loJson.UpdateString("crv","BP-256")
loJson.UpdateString("x","QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w")
loJson.UpdateString("y","AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu")

loPubkey = createobject("CkPublicKey")

llSuccess = loPubkey.LoadFromString(loJson.Emit())
if (llSuccess = .F.) then
    ? loPubkey.LastErrorText
    release loJson
    release loPubkey
    return
endif

// 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.
loJwt = createobject("CkJwt")

loJweProtHdr = createobject("CkJsonObject")
loJweProtHdr.UpdateString("alg","ECDH-ES")
loJweProtHdr.UpdateString("enc","A256GCM")
loJweProtHdr.UpdateInt("exp",loJwt.GenNumericDate(3600))
loJweProtHdr.UpdateString("cty","NJWT")
loJweProtHdr.UpdateString("epk.kty","EC")
loJweProtHdr.UpdateString("epk.x","QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w")
loJweProtHdr.UpdateString("epk.y","AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu")
loJweProtHdr.UpdateString("epk.crv","BP-256")

loJwe = createobject("CkJwe")
loJwe.SetProtectedHeader(loJweProtHdr)
loJwe.SetPublicKey(0,loPubkey)

lcPlainText = "This is the text to be encrypted."
lcStrJwe = loJwe.Encrypt(lcPlainText,"utf-8")
if (loJwe.LastMethodSuccess <> .T.) then
    ? loJwe.LastErrorText
    release loJson
    release loPubkey
    release loJwt
    release loJweProtHdr
    release loJwe
    return
endif

? lcStrJwe

? "Success."


release loJson
release loPubkey
release loJwt
release loJweProtHdr
release loJwe