Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(AutoIt) JWE Using Flattened JWE JSON SerializationThis example duplicates the example A.5 in RFC 7516 for JSON Web Encryption (JWE). This example demonstrates using the flattened JWE JSON Serialization syntax. This example demonstrates the capability for encrypting the plaintext to a single recipient in a flattened JSON structure. Note: This example requires Chilkat v9.5.0.66 or greater.
; 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. Local $bSuccess Local $sPlaintext = "Live long and prosper." $oJwe = ObjCreate("Chilkat.Jwe") ; First build the JWE Protected Header: {"enc":"A128CBC-HS256"} $oJweProtHdr = ObjCreate("Chilkat.JsonObject") $oJweProtHdr.AppendString("enc","A128CBC-HS256") $oJwe.SetProtectedHeader($oJweProtHdr) ; We have a single recipient that uses AES Key Wrap to encrypt the CEK. ; ; {"alg":"A128KW","kid":"7"} $oJweRecipientHdr = ObjCreate("Chilkat.JsonObject") $oJweRecipientHdr.AppendString("alg","A128KW") $oJweRecipientHdr.AppendString("kid","7") Local $iRecipientIndex = 0 $oJwe.SetRecipientHeader($iRecipientIndex,$oJweRecipientHdr) ; Set the Shared Unprotected Header: {"jku":"https://server.example.com/keys.jwks"} $oJweUnprotHdr = ObjCreate("Chilkat.JsonObject") $oJweUnprotHdr.AppendString("jku","https://server.example.com/keys.jwks") $oJwe.SetUnprotectedHeader($oJweUnprotHdr) ; Set the AES key wrapping key. Local $sAesWrappingKey = "GawgguFyGrWKav7AX4VKUg" $iRecipientIndex = 0 $oJwe.SetWrappingKey($iRecipientIndex,$sAesWrappingKey,"base64url") ; OK.. everything has been specified. ; Now encrypt. ; Indicate that we prefer the flattened JSON serialization. $oJwe.PreferFlattened = True Local $strJwe = $oJwe.Encrypt($sPlaintext,"utf-8") If ($oJwe.LastMethodSuccess <> True) Then ConsoleWrite($oJwe.LastErrorText & @CRLF) Exit EndIf ; The JWE is produced in the most compact form possible (a single line). ; Let's load it into a JSON object and examine in a non-compact pretty-printed format: $oJsonTemp = ObjCreate("Chilkat.JsonObject") $oJsonTemp.Load($strJwe) $oJsonTemp.EmitCompact = False ConsoleWrite($oJsonTemp.Emit() & @CRLF) ; The JWE looks like this: ; (Note: Because of random values used in the encryption process, your encrypted results will be different.) ; { ; "protected": "eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0", ; "unprotected": { ; "jku": "https://server.example.com/keys.jwks" ; }, ; "header": { ; "alg": "A128KW", ; "kid": "7" ; }, ; "encrypted_key": "FW7z9VxOvnF2ClicvUT4HTeqcdyh90C6qytfEFJsOb77FOwTfYrc3w", ; "iv": "O6Ly5-eML3zTs-_fNX3D5Q", ; "ciphertext": "Q8NLmeac9JhLh0CQPuG_7D9wVDb55eToCh0g5-FQ4M0", ; "tag": "slzlo6-J9C7wSDF4dh_kBg" ; } ; Now decrypt...................................... ; First, load the JWE.. $oJwe2 = ObjCreate("Chilkat.Jwe") $bSuccess = $oJwe2.LoadJwe($strJwe) If ($bSuccess <> True) Then ConsoleWrite($oJwe2.LastErrorText & @CRLF) Exit EndIf ; Set the AES wrap key.. $iRecipientIndex = 0 $oJwe2.SetWrappingKey($iRecipientIndex,$sAesWrappingKey,"base64url") ; Decrypt Local $sOriginalPlaintext = $oJwe2.Decrypt($iRecipientIndex,"utf-8") If ($oJwe2.LastMethodSuccess <> True) Then ConsoleWrite($oJwe2.LastErrorText & @CRLF) Exit EndIf ConsoleWrite("original text decrypted with AES key wrap key: " & @CRLF) ConsoleWrite($sOriginalPlaintext & @CRLF) ; --------------------------------------------------------------------------------- ; It should also be possible to decrypt the flattened JWE as shown in RFC 7516, Appendix A.5 ; because it was produced using the same key. $oSbJwe = ObjCreate("Chilkat.StringBuilder") $oSbJwe.Append("{") $oSbJwe.Append("""protected"":") $oSbJwe.Append("""eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0"",") $oSbJwe.Append("""unprotected"":") $oSbJwe.Append("{""jku"":""https://server.example.com/keys.jwks""},") $oSbJwe.Append("""header"":") $oSbJwe.Append("{""alg"":""A128KW"",""kid"":""7""},") $oSbJwe.Append("""encrypted_key"":") $oSbJwe.Append("""6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ"",") $oSbJwe.Append("""iv"":") $oSbJwe.Append("""AxY8DCtDaGlsbGljb3RoZQ"",") $oSbJwe.Append("""ciphertext"":") $oSbJwe.Append("""KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY"",") $oSbJwe.Append("""tag"":") $oSbJwe.Append("""Mz-VPPyU4RlcuYv1IwIvzw""") $oSbJwe.Append("}") $bSuccess = $oJwe2.LoadJweSb($oSbJwe) If ($bSuccess <> True) Then ConsoleWrite($oJwe2.LastErrorText & @CRLF) Exit EndIf $iRecipientIndex = 0 $oJwe2.SetWrappingKey($iRecipientIndex,$sAesWrappingKey,"base64url") $sOriginalPlaintext = $oJwe2.Decrypt($iRecipientIndex,"utf-8") If ($oJwe2.LastMethodSuccess <> True) Then ConsoleWrite($oJwe2.LastErrorText & @CRLF) Exit EndIf ConsoleWrite("original text decrypted from published flattened JWE: " & @CRLF) ConsoleWrite($sOriginalPlaintext & @CRLF) ; The output: ; original text decrypted with AES key wrap key: ; Live long and prosper. ; original text decrypted from published flattened JWE: ; Live long and prosper. |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.