AutoIt
AutoIt
Encrypt a JWE with a Recipient Public Key
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetPublicKey, which sets the public key used to encrypt for a recipient. The example uses RSA-OAEP-256 key management with AES-256-GCM content encryption.
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. In public-key JWE, the content-encryption key is wrapped for the recipient using their public key, so only the holder of the matching private key can decrypt.
Chilkat AutoIt Downloads
Local $bSuccess = False
$oJwe = ObjCreate("Chilkat.Jwe")
; Protected header: RSA-OAEP-256 key management with AES-256-GCM content encryption.
$oHeader = ObjCreate("Chilkat.JsonObject")
$oHeader.UpdateString("alg","RSA-OAEP-256")
$oHeader.UpdateString("enc","A256GCM")
$bSuccess = $oJwe.SetProtectedHeader($oHeader)
If ($bSuccess = False) Then
ConsoleWrite($oJwe.LastErrorText & @CRLF)
Exit
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 recipient's public key.
$oPubKey = ObjCreate("Chilkat.PublicKey")
$bSuccess = $oPubKey.LoadFromFile("qa_data/recipient_pubkey.pem")
If ($bSuccess = False) Then
ConsoleWrite($oPubKey.LastErrorText & @CRLF)
Exit
EndIf
; Set the public key used to encrypt for recipient 0.
$bSuccess = $oJwe.SetPublicKey(0,$oPubKey)
If ($bSuccess = False) Then
ConsoleWrite($oJwe.LastErrorText & @CRLF)
Exit
EndIf
Local $sJweCompact = $oJwe.Encrypt("This is the secret content.","utf-8")
If ($oJwe.LastMethodSuccess = False) Then
ConsoleWrite($oJwe.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite($sJweCompact & @CRLF)