AutoIt
AutoIt
Set a Per-Recipient JWE Header
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetRecipientHeader, which sets the per-recipient unprotected header for a recipient index.
Background. Per-recipient headers carry recipient-specific values such as a key id (
kid), and are used with the JSON serialization forms that support multiple recipients.Chilkat AutoIt Downloads
Local $bSuccess = False
$oJwe = ObjCreate("Chilkat.Jwe")
; Protected header: AES key-wrap with AES-256-GCM content encryption.
$oHeader = ObjCreate("Chilkat.JsonObject")
$oHeader.UpdateString("alg","A256KW")
$oHeader.UpdateString("enc","A256GCM")
$bSuccess = $oJwe.SetProtectedHeader($oHeader)
If ($bSuccess = False) Then
ConsoleWrite($oJwe.LastErrorText & @CRLF)
Exit
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.
Local $sBase64Key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
$bSuccess = $oJwe.SetWrappingKey(0,$sBase64Key,"base64")
If ($bSuccess = False) Then
ConsoleWrite($oJwe.LastErrorText & @CRLF)
Exit
EndIf
; Set the per-recipient unprotected header for recipient 0, for example a key id used to identify
; which key a recipient should use.
$oRecipHeader = ObjCreate("Chilkat.JsonObject")
$oRecipHeader.UpdateString("kid","recipient-key-1")
$bSuccess = $oJwe.SetRecipientHeader(0,$oRecipHeader)
If ($bSuccess = False) Then
ConsoleWrite($oJwe.LastErrorText & @CRLF)
Exit
EndIf
$oSbContent = ObjCreate("Chilkat.StringBuilder")
$oSbContent.Append("This is the secret content.")
$oSbJwe = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oJwe.EncryptSb($oSbContent,"utf-8",$oSbJwe)
If ($bSuccess = False) Then
ConsoleWrite($oJwe.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite($oSbJwe.GetAsString() & @CRLF)