Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set jwe [new_CkJwe]

#  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.
set sbJwe [new_CkStringBuilder]

set bLoaded [CkStringBuilder_LoadFile $sbJwe "qa_data/message.jwe" "utf-8"]
if {$bLoaded != 1} then {
    puts "Failed to load the JWE file."
    delete_CkJwe $jwe
    delete_CkStringBuilder $sbJwe
    exit
}

set success [CkJwe_LoadJweSb $jwe $sbJwe]
if {$success == 0} then {
    puts [CkJwe_lastErrorText $jwe]
    delete_CkJwe $jwe
    delete_CkStringBuilder $sbJwe
    exit
}

#  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.
set bCaseSensitive 1
set index [CkJwe_FindRecipient $jwe "kid" "recipient-key-1" $bCaseSensitive]
if {$index < 0} then {
    puts "Recipient not found."
    delete_CkJwe $jwe
    delete_CkStringBuilder $sbJwe
    exit
}

puts "Found recipient at index $index"

delete_CkJwe $jwe
delete_CkStringBuilder $sbJwe