PureBasic
PureBasic
Decrypt a JWE into a StringBuilder
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.DecryptSb, which decrypts a recipient of a loaded JWE and writes the plaintext into a StringBuilder.
Background. The recipient index selects which recipient's key material to use. The key for that recipient must be provided before decrypting.
Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.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 into a StringBuilder.
sbContent.i = CkStringBuilder::ckCreate()
If sbContent.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJwe::ckDecryptSb(jwe,0,"utf-8",sbContent)
If success = 0
Debug CkJwe::ckLastErrorText(jwe)
CkJwe::ckDispose(jwe)
CkStringBuilder::ckDispose(sbContent)
ProcedureReturn
EndIf
Debug CkStringBuilder::ckGetAsString(sbContent)
CkJwe::ckDispose(jwe)
CkStringBuilder::ckDispose(sbContent)
ProcedureReturn
EndProcedure