AutoIt
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; 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.
$oJpgBytes = ObjCreate("Chilkat.BinData")
$bSuccess = $oJpgBytes.LoadFile("qa_data/jpg/starfish.jpg")
; Make sure your app checks the success/failure of the call to LoadFile..
ConsoleWrite("Original JPG size = " & $oJpgBytes.NumBytes & @CRLF)
$oJwe = ObjCreate("Chilkat.Jwe")
$oJweProtHdr = ObjCreate("Chilkat.JsonObject")
$oJweProtHdr.AppendString("alg","A128KW")
$oJweProtHdr.AppendString("enc","A128CBC-HS256")
$oJwe.SetProtectedHeader($oJweProtHdr)
Local $sAesWrappingKey = "GawgguFyGrWKav7AX4VKUg"
$oJwe.SetWrappingKey(0,$sAesWrappingKey,"base64url")
; Encrypt and return the JWE in sbJwe:
$oSbJwe = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oJwe.EncryptBd($oJpgBytes,$oSbJwe)
If ($bSuccess <> True) Then
ConsoleWrite($oJwe.LastErrorText & @CRLF)
Exit
EndIf
; Show the JWE:
ConsoleWrite($oSbJwe.GetAsString() & @CRLF)
ConsoleWrite("size of JWE: " & $oSbJwe.Length & @CRLF)
; ---------------------------------------------------------
; Decrypt to get the original JPG file..
$oJwe2 = ObjCreate("Chilkat.Jwe")
$bSuccess = $oJwe2.LoadJweSb($oSbJwe)
If ($bSuccess <> True) Then
ConsoleWrite($oJwe2.LastErrorText & @CRLF)
Exit
EndIf
; Set the AES wrap key.
$oJwe2.SetWrappingKey(0,$sAesWrappingKey,"base64url")
; Decrypt.
$oJpgOriginal = ObjCreate("Chilkat.BinData")
$bSuccess = $oJwe2.DecryptBd(0,$oJpgOriginal)
If ($bSuccess <> True) Then
ConsoleWrite($oJwe2.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Decrypted JPG size = " & $oJpgOriginal.NumBytes & @CRLF)
; Save the decrypted JPG to a file.
$bSuccess = $oJpgOriginal.WriteFile("qa_output/jwe_decrypted_starfish.jpg")
ConsoleWrite("success = " & $bSuccess & @CRLF)
; 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