Lua
Lua
Encrypt a JWE with a Recipient Public Key
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetPublicKey, which sets the public key used to encrypt for a recipient. The example uses RSA-OAEP-256 key management with AES-256-GCM content encryption.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. In public-key JWE, the content-encryption key is wrapped for the recipient using their public key, so only the holder of the matching private key can decrypt.
Chilkat Lua Downloads
local chilkat = require("chilkat")
local jwe = chilkat.Jwe.new()
-- Protected header: RSA-OAEP-256 key management with AES-256-GCM content encryption.
local header = chilkat.JsonObject.new()
header:update_string("alg", "RSA-OAEP-256")
header:update_string("enc", "A256GCM")
local success = jwe:set_protected_header(header)
if not success then
print(jwe.last_error_text)
return
end
-- The file path is relative to the application's current working directory. An absolute path
-- may also be used. Supply the path appropriate to your own environment.
-- Load the recipient's public key.
local pubKey = chilkat.PublicKey.new()
success = pubKey:load_from_file("qa_data/recipient_pubkey.pem")
if not success then
print(pubKey.last_error_text)
return
end
-- Set the public key used to encrypt for recipient 0.
success = jwe:set_public_key(0, pubKey)
if not success then
print(jwe.last_error_text)
return
end
local jweCompact = jwe:encrypt("This is the secret content.", "utf-8")
if not jwe.last_method_success then
print(jwe.last_error_text)
return
end
print(jweCompact)