Sample code for 30+ languages & platforms
PowerBuilder

Encrypt Content to a JWE

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.Encrypt, which encrypts a string and returns the resulting JWE. The charset argument converts the text to bytes before encryption.

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jwe
oleobject loo_Header
string ls_Base64Key
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

//  Set the JWE protected header, specifying the key-management algorithm (alg) and the content-
//  encryption algorithm (enc).
loo_Header = create oleobject
li_rc = loo_Header.ConnectToNewObject("Chilkat.JsonObject")

loo_Header.UpdateString("alg","A256KW")
loo_Header.UpdateString("enc","A256GCM")
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

//  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.
ls_Base64Key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
li_Success = loo_Jwe.SetWrappingKey(0,ls_Base64Key,"base64")
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_Header
    return
end if

//  Encrypt the content.  The 2nd argument is the charset used to convert the text to bytes.  The
//  returned string is the JWE (compact serialization by default).
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