Sample code for 30+ languages & platforms
PowerBuilder

JWE with Binary Data

See more JSON Web Encryption (JWE) Examples

Demonstrates how to create a JWE that contains a binary payload (such as a JPG image).

Note: This example requires Chilkat v9.5.0.66 or greater.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_JpgBytes
oleobject loo_Jwe
oleobject loo_JweProtHdr
string ls_AesWrappingKey
oleobject loo_SbJwe
oleobject loo_Jwe2
oleobject loo_JpgOriginal

li_Success = 0

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Note: This example requires Chilkat v9.5.0.66 or greater.

// Load a JPG file that will be the JWE payload.
loo_JpgBytes = create oleobject
li_rc = loo_JpgBytes.ConnectToNewObject("Chilkat.BinData")
if li_rc < 0 then
    destroy loo_JpgBytes
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_JpgBytes.LoadFile("qa_data/jpg/starfish.jpg")
// Make sure your app checks the success/failure of the call to LoadFile..
Write-Debug "Original JPG size = " + string(loo_JpgBytes.NumBytes)

loo_Jwe = create oleobject
li_rc = loo_Jwe.ConnectToNewObject("Chilkat.Jwe")

loo_JweProtHdr = create oleobject
li_rc = loo_JweProtHdr.ConnectToNewObject("Chilkat.JsonObject")

loo_JweProtHdr.AppendString("alg","A128KW")
loo_JweProtHdr.AppendString("enc","A128CBC-HS256")
loo_Jwe.SetProtectedHeader(loo_JweProtHdr)

ls_AesWrappingKey = "GawgguFyGrWKav7AX4VKUg"
loo_Jwe.SetWrappingKey(0,ls_AesWrappingKey,"base64url")

// Encrypt and return the JWE in sbJwe:
loo_SbJwe = create oleobject
li_rc = loo_SbJwe.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Jwe.EncryptBd(loo_JpgBytes,loo_SbJwe)
if li_Success <> 1 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_JpgBytes
    destroy loo_Jwe
    destroy loo_JweProtHdr
    destroy loo_SbJwe
    return
end if

// Show the JWE:
Write-Debug loo_SbJwe.GetAsString()
Write-Debug "size of JWE: " + string(loo_SbJwe.Length)

// ---------------------------------------------------------
// Decrypt to get the original JPG file..

loo_Jwe2 = create oleobject
li_rc = loo_Jwe2.ConnectToNewObject("Chilkat.Jwe")

li_Success = loo_Jwe2.LoadJweSb(loo_SbJwe)
if li_Success <> 1 then
    Write-Debug loo_Jwe2.LastErrorText
    destroy loo_JpgBytes
    destroy loo_Jwe
    destroy loo_JweProtHdr
    destroy loo_SbJwe
    destroy loo_Jwe2
    return
end if

// Set the AES wrap key.
loo_Jwe2.SetWrappingKey(0,ls_AesWrappingKey,"base64url")

// Decrypt.
loo_JpgOriginal = create oleobject
li_rc = loo_JpgOriginal.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Jwe2.DecryptBd(0,loo_JpgOriginal)
if li_Success <> 1 then
    Write-Debug loo_Jwe2.LastErrorText
    destroy loo_JpgBytes
    destroy loo_Jwe
    destroy loo_JweProtHdr
    destroy loo_SbJwe
    destroy loo_Jwe2
    destroy loo_JpgOriginal
    return
end if

Write-Debug "Decrypted JPG size = " + string(loo_JpgOriginal.NumBytes)

// Save the decrypted JPG to a file.
li_Success = loo_JpgOriginal.WriteFile("qa_output/jwe_decrypted_starfish.jpg")

Write-Debug "success = " + string(li_Success)

// The output of this program, when tested, was:
// Original JPG size = 6229
// eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.9YFz_wteV ... 7Et3hKhpxnKEXw
// size of JWE: 8473
// Decrypted JPG size = 6229
// success = True


destroy loo_JpgBytes
destroy loo_Jwe
destroy loo_JweProtHdr
destroy loo_SbJwe
destroy loo_Jwe2
destroy loo_JpgOriginal