Sample code for 30+ languages & platforms
AutoIt

Decrypt a JWE with a Private Key

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetPrivateKey, which sets the private key used to decrypt a recipient of a loaded JWE.

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. The private key unwraps the content-encryption key that was wrapped with the corresponding public key during encryption.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

$oJwe = ObjCreate("Chilkat.Jwe")

;  Load the JWE compact serialization to be decrypted.
Local $sJweCompact = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>"
$bSuccess = $oJwe.LoadJwe($sJweCompact)
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 private key.
$oPrivKey = ObjCreate("Chilkat.PrivateKey")
$bSuccess = $oPrivKey.LoadPemFile("qa_data/recipient_privkey.pem")
If ($bSuccess = False) Then
    ConsoleWrite($oPrivKey.LastErrorText & @CRLF)
    Exit
EndIf

;  Set the private key used to decrypt recipient 0.
$bSuccess = $oJwe.SetPrivateKey(0,$oPrivKey)
If ($bSuccess = False) Then
    ConsoleWrite($oJwe.LastErrorText & @CRLF)
    Exit
EndIf

Local $sContent = $oJwe.Decrypt(0,"utf-8")
If ($oJwe.LastMethodSuccess = False) Then
    ConsoleWrite($oJwe.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($sContent & @CRLF)