PureBasic
PureBasic
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 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
; 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.
sbJwe.i = CkStringBuilder::ckCreate()
If sbJwe.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
bLoaded.i = CkStringBuilder::ckLoadFile(sbJwe,"qa_data/message.jwe","utf-8")
If bLoaded <> 1
Debug "Failed to load the JWE file."
CkJwe::ckDispose(jwe)
CkStringBuilder::ckDispose(sbJwe)
ProcedureReturn
EndIf
; Load the JWE from the StringBuilder.
success = CkJwe::ckLoadJweSb(jwe,sbJwe)
If success = 0
Debug CkJwe::ckLastErrorText(jwe)
CkJwe::ckDispose(jwe)
CkStringBuilder::ckDispose(sbJwe)
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)
CkStringBuilder::ckDispose(sbJwe)
ProcedureReturn
EndIf
content.s = CkJwe::ckDecrypt(jwe,0,"utf-8")
If CkJwe::ckLastMethodSuccess(jwe) = 0
Debug CkJwe::ckLastErrorText(jwe)
CkJwe::ckDispose(jwe)
CkStringBuilder::ckDispose(sbJwe)
ProcedureReturn
EndIf
Debug content
CkJwe::ckDispose(jwe)
CkStringBuilder::ckDispose(sbJwe)
ProcedureReturn
EndProcedure