Sample code for 30+ languages & platforms
PowerBuilder

Load a JWE from a StringBuilder

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.LoadJweSb, which loads a JWE from a StringBuilder (here read from a file) so it can be decrypted.

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. 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_SbJwe
integer li_BLoaded
string ls_Base64Key
string ls_Content

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

//  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 JWE text from a file into a StringBuilder.
loo_SbJwe = create oleobject
li_rc = loo_SbJwe.ConnectToNewObject("Chilkat.StringBuilder")

li_BLoaded = loo_SbJwe.LoadFile("qa_data/message.jwe","utf-8")
if li_BLoaded <> 1 then
    Write-Debug "Failed to load the JWE file."
    destroy loo_Jwe
    destroy loo_SbJwe
    return
end if

//  Load the JWE from the StringBuilder.
li_Success = loo_Jwe.LoadJweSb(loo_SbJwe)
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_SbJwe
    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_SbJwe
    return
end if

ls_Content = loo_Jwe.Decrypt(0,"utf-8")
if loo_Jwe.LastMethodSuccess = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_SbJwe
    return
end if

Write-Debug ls_Content


destroy loo_Jwe
destroy loo_SbJwe