Lua Requires Chilkat v9.5.0.87+
Lua
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"
}
}
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- 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"
-- }
local json = chilkat.JsonObject.new()
json:update_string("use", "enc")
json:update_string("kid", "puk_idp_enc")
json:update_string("kty", "EC")
json:update_string("crv", "BP-256")
json:update_string("x", "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w")
json:update_string("y", "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu")
local pubkey = chilkat.PublicKey.new()
local success = pubkey:load_from_string(json:emit())
if not success then
print(pubkey.last_error_text)
return
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.
local jwt = chilkat.Jwt.new()
local jweProtHdr = chilkat.JsonObject.new()
jweProtHdr:update_string("alg", "ECDH-ES")
jweProtHdr:update_string("enc", "A256GCM")
jweProtHdr:update_int("exp", jwt:gen_numeric_date(3600))
jweProtHdr:update_string("cty", "NJWT")
jweProtHdr:update_string("epk.kty", "EC")
jweProtHdr:update_string("epk.x", "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w")
jweProtHdr:update_string("epk.y", "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu")
jweProtHdr:update_string("epk.crv", "BP-256")
local jwe = chilkat.Jwe.new()
jwe:set_protected_header(jweProtHdr)
jwe:set_public_key(0, pubkey)
local plainText = "This is the text to be encrypted."
local strJwe = jwe:encrypt(plainText, "utf-8")
if not jwe.last_method_success then
print(jwe.last_error_text)
return
end
print(strJwe)
print("Success.")