Sample code for 30+ languages & platforms
Lua

Decrypt a JWE with a Private Key

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetPrivateKey, which sets the private key used to decrypt a recipient of a loaded JWE.

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. The private key unwraps the content-encryption key that was wrapped with the corresponding public key during encryption.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local jwe = chilkat.Jwe.new()

-- Load the JWE compact serialization to be decrypted.
local jweCompact = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>"
local success = jwe:load_jwe(jweCompact)
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 private key.
local privKey = chilkat.PrivateKey.new()
success = privKey:load_pem_file("qa_data/recipient_privkey.pem")
if not success then
    print(privKey.last_error_text)
    return
end

-- Set the private key used to decrypt recipient 0.
success = jwe:set_private_key(0, privKey)
if not success then
    print(jwe.last_error_text)
    return
end

local content = jwe:decrypt(0, "utf-8")
if not jwe.last_method_success then
    print(jwe.last_error_text)
    return
end

print(content)