Sample code for 30+ languages & platforms
PowerBuilder

Decrypt a JWE into BinData

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.DecryptBd, which decrypts a recipient of a loaded JWE and writes the plaintext bytes into a BinData.

Background. This is used when the decrypted content is binary. The recipient key must be provided before decrypting.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jwe
string ls_JweCompact
string ls_Base64Key
oleobject loo_Bd

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

//  Load the JWE compact serialization to be decrypted.
ls_JweCompact = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>"
li_Success = loo_Jwe.LoadJwe(ls_JweCompact)
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    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
    return
end if

//  Decrypt recipient index 0, writing the plaintext bytes into a BinData.
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Jwe.DecryptBd(0,loo_Bd)
if li_Success = 0 then
    Write-Debug loo_Jwe.LastErrorText
    destroy loo_Jwe
    destroy loo_Bd
    return
end if

Write-Debug "Decrypted " + string(loo_Bd.NumBytes) + " bytes."


destroy loo_Jwe
destroy loo_Bd