Sample code for 30+ languages & platforms
Tcl

Load a JWE from a StringBuilder

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.LoadJweSb, which loads a JWE from a StringBuilder (here read from a file) so it can be decrypted.

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. JWE (JSON Web Encryption) protects content with a content-encryption algorithm (the header enc value) while the content-encryption key is managed by the key-management algorithm (the header alg value). This example uses AES key wrapping with a symmetric wrapping key.

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 the JWE text from a file into a StringBuilder.
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
}

#  Load the JWE from the StringBuilder.
set success [CkJwe_LoadJweSb $jwe $sbJwe]
if {$success == 0} then {
    puts [CkJwe_lastErrorText $jwe]
    delete_CkJwe $jwe
    delete_CkStringBuilder $sbJwe
    exit
}

#  The 256-bit key-wrapping key (base64) for recipient index 0.  In production, obtain the key
#  from a secure source rather than hard-coding it.
set base64Key "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
set success [CkJwe_SetWrappingKey $jwe 0 $base64Key "base64"]
if {$success == 0} then {
    puts [CkJwe_lastErrorText $jwe]
    delete_CkJwe $jwe
    delete_CkStringBuilder $sbJwe
    exit
}

set content [CkJwe_decrypt $jwe 0 "utf-8"]
if {[CkJwe_get_LastMethodSuccess $jwe] == 0} then {
    puts [CkJwe_lastErrorText $jwe]
    delete_CkJwe $jwe
    delete_CkStringBuilder $sbJwe
    exit
}

puts "$content"

delete_CkJwe $jwe
delete_CkStringBuilder $sbJwe