Sample code for 30+ languages & platforms
Lua

Decrypt a JWE to a String

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.Decrypt, which decrypts a recipient of a loaded JWE and returns the plaintext, decoded using the given charset. Before decrypting, the example inspects the JWE protected header and, under application policy, accepts only permitted algorithms and selects an acceptable recipient.

Background. A received JWE should never be trusted to specify safe algorithms. The example reads the alg and enc values from the protected header (via GetProtectedHeader) and enforces an allowlist, rejecting anything not permitted. It then selects the recipient the application holds a key for — using FindRecipient to locate a recipient by a per-recipient header value such as kid, falling back to index 0 for a single-recipient compact JWE — and only then supplies the key and decrypts.

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

-- Application policy: before decrypting, inspect the JWE header and accept only algorithms the
-- application trusts.  Never assume the algorithms in a received JWE are safe -- a sender could
-- specify a weak or unexpected algorithm.
local protHeader = chilkat.JsonObject.new()
success = jwe:get_protected_header(protHeader)
if not success then
    print(jwe.last_error_text)
    return
end

-- Read the alg (key-management) and enc (content-encryption) header values into StringBuilder
-- objects so they can be compared.
local sbAlg = chilkat.StringBuilder.new()
success = protHeader:string_of_sb("alg", sbAlg)
if not success then
    print(protHeader.last_error_text)
    return
end

local sbEnc = chilkat.StringBuilder.new()
success = protHeader:string_of_sb("enc", sbEnc)
if not success then
    print(protHeader.last_error_text)
    return
end

print("JWE algorithms: alg=" .. sbAlg:get_as_string() .. " enc=" .. sbEnc:get_as_string())

-- Enforce an allowlist of acceptable algorithms.  Reject anything not permitted by policy.  String
-- values are compared using StringBuilder.ContentsEqual.
local bCaseSensitive = true
if not sbAlg:contents_equal("A256KW", bCaseSensitive) then
    print("Rejecting JWE: key-management algorithm is not permitted by policy.")
    return
end

if not sbEnc:contents_equal("A256GCM", bCaseSensitive) then
    print("Rejecting JWE: content-encryption algorithm is not permitted by policy.")
    return
end

-- Select an acceptable recipient.  For a multi-recipient JWE, FindRecipient locates the recipient
-- this application holds a key for by a per-recipient header value such as "kid".  A compact JWE
-- has a single recipient at index 0, so FindRecipient returns -1; fall back to index 0 in that case.
local recipientIndex = jwe:find_recipient("kid", "recipient-key-1", bCaseSensitive)
if recipientIndex < 0 then
    recipientIndex = 0
end

-- Provide the key for the selected recipient.  In production, obtain the key from a secure source
-- rather than hard-coding it.
local base64Key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
success = jwe:set_wrapping_key(recipientIndex, base64Key, "base64")
if not success then
    print(jwe.last_error_text)
    return
end

-- Decrypt the selected recipient and return the plaintext, decoded using the given charset.
local content = jwe:decrypt(recipientIndex, "utf-8")
if not jwe.last_method_success then
    print(jwe.last_error_text)
    return
end

print(content)