Sample code for 30+ languages & platforms
Tcl

Encrypt a JWE with a Password (PBES2)

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetPassword, which sets the PBES2 password for a recipient. The example uses PBES2-HS256+A128KW key management with AES-128-GCM content encryption.

Background. PBES2 derives a key-wrapping key from a password using a salt and iteration count, allowing password-based JWE encryption. The password should come from a secure source.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set jwe [new_CkJwe]

#  Protected header: PBES2 password-based key management with AES-128-GCM content encryption.
set header [new_CkJsonObject]

CkJsonObject_UpdateString $header "alg" "PBES2-HS256+A128KW"
CkJsonObject_UpdateString $header "enc" "A128GCM"
set success [CkJwe_SetProtectedHeader $jwe $header]
if {$success == 0} then {
    puts [CkJwe_lastErrorText $jwe]
    delete_CkJwe $jwe
    delete_CkJsonObject $header
    exit
}

#  Set the PBES2 password for recipient 0.  The password should come from a secure source rather than
#  being hard-coded.
set password "myPassword"
set success [CkJwe_SetPassword $jwe 0 $password]
if {$success == 0} then {
    puts [CkJwe_lastErrorText $jwe]
    delete_CkJwe $jwe
    delete_CkJsonObject $header
    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
    exit
}

puts "$jweCompact"

delete_CkJwe $jwe
delete_CkJsonObject $header