Sample code for 30+ languages & platforms
CkPython

JWE using ECDH-ES+A256KW

See more JSON Web Encryption (JWE) Examples

Create a JWE with the following public/private key pair:
{
    "kty": "EC",
    "d": "jZCffzVqJjryBH4EoaN0oD-TyLXrW2XHoDdIuPZnk8c",
    "use": "enc",
    "crv": "P-256",
    "kid": "evEK2thJMsWxBYRivXI8ykUf6n6zizLiLCGH3s58wKs",
    "x": "LOakgGvxWBsWbCPLY6Vq6OuBktIqG8POXFXe7ngQ2oM",
    "y": "voJvS6I-Mc4qqmEA_G2hLQqBck3a3vqaJbmzY7YPUD4",
    "alg": "ECDH-ES+A256KW"
}

Also shows how to decrypt.

Chilkat CkPython Downloads

CkPython
import sys
import chilkat

success = False

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

# Create the following JSON:
# {
#     "kty": "EC",
#     "d": "jZCffzVqJjryBH4EoaN0oD-TyLXrW2XHoDdIuPZnk8c",
#     "use": "enc",
#     "crv": "P-256",
#     "kid": "evEK2thJMsWxBYRivXI8ykUf6n6zizLiLCGH3s58wKs",
#     "x": "LOakgGvxWBsWbCPLY6Vq6OuBktIqG8POXFXe7ngQ2oM",
#     "y": "voJvS6I-Mc4qqmEA_G2hLQqBck3a3vqaJbmzY7YPUD4",
#     "alg": "ECDH-ES+A256KW"
# }

json = chilkat.CkJsonObject()
json.UpdateString("kty","EC")
json.UpdateString("d","jZCffzVqJjryBH4EoaN0oD-TyLXrW2XHoDdIuPZnk8c")
json.UpdateString("use","enc")
json.UpdateString("crv","P-256")
json.UpdateString("kid","evEK2thJMsWxBYRivXI8ykUf6n6zizLiLCGH3s58wKs")
json.UpdateString("x","LOakgGvxWBsWbCPLY6Vq6OuBktIqG8POXFXe7ngQ2oM")
json.UpdateString("y","voJvS6I-Mc4qqmEA_G2hLQqBck3a3vqaJbmzY7YPUD4")
json.UpdateString("alg","ECDH-ES+A256KW")

pubkey = chilkat.CkPublicKey()

success = pubkey.LoadFromString(json.emit())
if (success == False):
    print(pubkey.lastErrorText())
    sys.exit()

# Build our protected header:

# 	{
# 	  "alg": "ECDH-ES+A256KW",
# 	  "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 = chilkat.CkJwt()

jweProtHdr = chilkat.CkJsonObject()
jweProtHdr.UpdateString("alg","ECDH-ES+A256KW")
jweProtHdr.UpdateString("enc","A256GCM")
jweProtHdr.UpdateInt("exp",jwt.GenNumericDate(3600))
jweProtHdr.UpdateString("cty","NJWT")
jweProtHdr.UpdateString("epk.kty","EC")
jweProtHdr.UpdateString("epk.x","LOakgGvxWBsWbCPLY6Vq6OuBktIqG8POXFXe7ngQ2oM")
jweProtHdr.UpdateString("epk.y","voJvS6I-Mc4qqmEA_G2hLQqBck3a3vqaJbmzY7YPUD4")
jweProtHdr.UpdateString("epk.crv","P-256")

jwe = chilkat.CkJwe()
jwe.SetProtectedHeader(jweProtHdr)
jwe.SetPublicKey(0,pubkey)

plainText = "This is the text to be encrypted."
strJwe = jwe.encrypt(plainText,"utf-8")
if (jwe.get_LastMethodSuccess() != True):
    print(jwe.lastErrorText())
    sys.exit()

print(strJwe)

# Let's decrypt...
privkey = chilkat.CkPrivateKey()

success = privkey.LoadJwk(json.emit())
if (success == False):
    print(privkey.lastErrorText())
    sys.exit()

jwe2 = chilkat.CkJwe()
success = jwe2.LoadJwe(strJwe)
if (success == False):
    print(jwe2.lastErrorText())
    sys.exit()

jwe2.SetPrivateKey(0,privkey)

#  Decrypt.
decryptedText = jwe2.decrypt(0,"utf-8")
if (jwe2.get_LastMethodSuccess() != True):
    print(jwe2.lastErrorText())
    sys.exit()

print(decryptedText)