Lua
Lua
Encrypt a JWE with a Password (PBES2)
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetPassword, which sets the PBES2 password for a recipient. The example uses PBES2-HS256+A128KW key management with AES-128-GCM content encryption.
Background. PBES2 derives a key-wrapping key from a password using a salt and iteration count, allowing password-based JWE encryption. The password should come from a secure source.
Chilkat Lua Downloads
local chilkat = require("chilkat")
local jwe = chilkat.Jwe.new()
-- Protected header: PBES2 password-based key management with AES-128-GCM content encryption.
local header = chilkat.JsonObject.new()
header:update_string("alg", "PBES2-HS256+A128KW")
header:update_string("enc", "A128GCM")
local success = jwe:set_protected_header(header)
if not success then
print(jwe.last_error_text)
return
end
-- Set the PBES2 password for recipient 0. The password should come from a secure source rather than
-- being hard-coded.
local password = "myPassword"
success = jwe:set_password(0, password)
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)