Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

set jwe [new_CkJwe]

#  Load the JWE compact serialization to be decrypted.
set jweCompact "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>"
set success [CkJwe_LoadJwe $jwe $jweCompact]
if {$success == 0} then {
    puts [CkJwe_lastErrorText $jwe]
    delete_CkJwe $jwe
    exit
}

#  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.
set privKey [new_CkPrivateKey]

set success [CkPrivateKey_LoadPemFile $privKey "qa_data/recipient_privkey.pem"]
if {$success == 0} then {
    puts [CkPrivateKey_lastErrorText $privKey]
    delete_CkJwe $jwe
    delete_CkPrivateKey $privKey
    exit
}

#  Set the private key used to decrypt recipient 0.
set success [CkJwe_SetPrivateKey $jwe 0 $privKey]
if {$success == 0} then {
    puts [CkJwe_lastErrorText $jwe]
    delete_CkJwe $jwe
    delete_CkPrivateKey $privKey
    exit
}

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

puts "$content"

delete_CkJwe $jwe
delete_CkPrivateKey $privKey