PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkBinData.pb"
IncludeFile "CkJwe.pb"
Procedure ChilkatExample()
success.i = 0
jwe.i = CkJwe::ckCreate()
If jwe.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Load the JWE compact serialization to be decrypted.
jweCompact.s = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>"
success = CkJwe::ckLoadJwe(jwe,jweCompact)
If success = 0
Debug CkJwe::ckLastErrorText(jwe)
CkJwe::ckDispose(jwe)
ProcedureReturn
EndIf
; 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.
base64Key.s = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
success = CkJwe::ckSetWrappingKey(jwe,0,base64Key,"base64")
If success = 0
Debug CkJwe::ckLastErrorText(jwe)
CkJwe::ckDispose(jwe)
ProcedureReturn
EndIf
; Decrypt recipient index 0, writing the plaintext bytes into a BinData.
bd.i = CkBinData::ckCreate()
If bd.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJwe::ckDecryptBd(jwe,0,bd)
If success = 0
Debug CkJwe::ckLastErrorText(jwe)
CkJwe::ckDispose(jwe)
CkBinData::ckDispose(bd)
ProcedureReturn
EndIf
Debug "Decrypted " + Str(CkBinData::ckNumBytes(bd)) + " bytes."
CkJwe::ckDispose(jwe)
CkBinData::ckDispose(bd)
ProcedureReturn
EndProcedure