Tcl
Tcl
Encrypt a JWE with a Recipient Public Key
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetPublicKey, which sets the public key used to encrypt for a recipient. The example uses RSA-OAEP-256 key management with AES-256-GCM content encryption.
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. In public-key JWE, the content-encryption key is wrapped for the recipient using their public key, so only the holder of the matching private key can decrypt.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
set jwe [new_CkJwe]
# Protected header: RSA-OAEP-256 key management with AES-256-GCM content encryption.
set header [new_CkJsonObject]
CkJsonObject_UpdateString $header "alg" "RSA-OAEP-256"
CkJsonObject_UpdateString $header "enc" "A256GCM"
set success [CkJwe_SetProtectedHeader $jwe $header]
if {$success == 0} then {
puts [CkJwe_lastErrorText $jwe]
delete_CkJwe $jwe
delete_CkJsonObject $header
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 public key.
set pubKey [new_CkPublicKey]
set success [CkPublicKey_LoadFromFile $pubKey "qa_data/recipient_pubkey.pem"]
if {$success == 0} then {
puts [CkPublicKey_lastErrorText $pubKey]
delete_CkJwe $jwe
delete_CkJsonObject $header
delete_CkPublicKey $pubKey
exit
}
# Set the public key used to encrypt for recipient 0.
set success [CkJwe_SetPublicKey $jwe 0 $pubKey]
if {$success == 0} then {
puts [CkJwe_lastErrorText $jwe]
delete_CkJwe $jwe
delete_CkJsonObject $header
delete_CkPublicKey $pubKey
exit
}
set jweCompact [CkJwe_encrypt $jwe "This is the secret content." "utf-8"]
if {[CkJwe_get_LastMethodSuccess $jwe] == 0} then {
puts [CkJwe_lastErrorText $jwe]
delete_CkJwe $jwe
delete_CkJsonObject $header
delete_CkPublicKey $pubKey
exit
}
puts "$jweCompact"
delete_CkJwe $jwe
delete_CkJsonObject $header
delete_CkPublicKey $pubKey