Sample code for 30+ languages & platforms
PowerBuilder

Set the Shared JWE Unprotected Header

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetUnprotectedHeader, which sets the shared JWE unprotected header from a JsonObject.

Background. The unprotected header is shared by all recipients but, unlike the protected header, is not integrity-protected. It is carried in the JSON serialization forms of a JWE.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jwe
oleobject loo_Header
string ls_Base64Key
oleobject loo_Unprotected
oleobject loo_SbContent
oleobject loo_SbJwe

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: AES key-wrap with AES-256-GCM content encryption.
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

//  Set the shared JWE unprotected header.  It is shared by all recipients but, unlike the protected
//  header, is not integrity-protected.
loo_Unprotected = create oleobject
li_rc = loo_Unprotected.ConnectToNewObject("Chilkat.JsonObject")

loo_Unprotected.UpdateString("cty","text/plain")
li_Success = loo_Jwe.SetUnprotectedHeader(loo_Unprotected)
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_Header
    destroy loo_Unprotected
    return
end if

loo_SbContent = create oleobject
li_rc = loo_SbContent.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbContent.Append("This is the secret content.")
loo_SbJwe = create oleobject
li_rc = loo_SbJwe.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Jwe.EncryptSb(loo_SbContent,"utf-8",loo_SbJwe)
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_Header
    destroy loo_Unprotected
    destroy loo_SbContent
    destroy loo_SbJwe
    return
end if

Write-Debug loo_SbJwe.GetAsString()


destroy loo_Jwe
destroy loo_Header
destroy loo_Unprotected
destroy loo_SbContent
destroy loo_SbJwe