Sample code for 30+ languages & platforms
Lua

Find a JWE Recipient by Header Value

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.FindRecipient, which searches the per-recipient unprotected headers for a member with a given name and string value, and returns the matching recipient index.

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. This locates the recipient that a particular key belongs to (for example by kid) before decrypting. The method returns -1 when no match is found.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local jwe = chilkat.Jwe.new()

-- 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 a multi-recipient JWE (JSON serialization) from a file.
local sbJwe = chilkat.StringBuilder.new()
local bLoaded = sbJwe:load_file("qa_data/message.jwe", "utf-8")
if not bLoaded then
    print("Failed to load the JWE file.")
    return
end

local success = jwe:load_jwe_sb(sbJwe)
if not success then
    print(jwe.last_error_text)
    return
end

-- Search the per-recipient unprotected headers for a member named "kid" whose value equals the
-- given string.  Returns the recipient index, or -1 if not found.
local bCaseSensitive = true
local index = jwe:find_recipient("kid", "recipient-key-1", bCaseSensitive)
if index < 0 then
    print("Recipient not found.")
    return
end

print("Found recipient at index " .. index)