Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jwe
oleobject loo_Header
string ls_Password
string ls_JweCompact

li_Success = 0

loo_Jwe = create oleobject
li_rc = loo_Jwe.ConnectToNewObject("Chilkat.Jwe")
if li_rc < 0 then
    destroy loo_Jwe
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Protected header: PBES2 password-based key management with AES-128-GCM content encryption.
loo_Header = create oleobject
li_rc = loo_Header.ConnectToNewObject("Chilkat.JsonObject")

loo_Header.UpdateString("alg","PBES2-HS256+A128KW")
loo_Header.UpdateString("enc","A128GCM")
li_Success = loo_Jwe.SetProtectedHeader(loo_Header)
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_Header
    return
end if

//  Set the PBES2 password for recipient 0.  The password should come from a secure source rather than
//  being hard-coded.
ls_Password = "myPassword"
li_Success = loo_Jwe.SetPassword(0,ls_Password)
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_Header
    return
end if

ls_JweCompact = loo_Jwe.Encrypt("This is the secret content.","utf-8")
if loo_Jwe.LastMethodSuccess = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_Header
    return
end if

Write-Debug ls_JweCompact


destroy loo_Jwe
destroy loo_Header