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
(PowerBuilder) 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.
integer li_rc integer li_Success string ls_Plaintext oleobject loo_Jwe oleobject loo_JweProtHdr oleobject loo_JweRecipientHdr integer li_RecipientIndex oleobject loo_JweUnprotHdr string ls_AesWrappingKey string ls_StrJwe oleobject loo_JsonTemp oleobject loo_Jwe2 string ls_OriginalPlaintext oleobject loo_SbJwe // 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. ls_Plaintext = "Live long and prosper." loo_Jwe = create oleobject // Use "Chilkat_9_5_0.Jwe" for versions of Chilkat < 10.0.0 li_rc = loo_Jwe.ConnectToNewObject("Chilkat.Jwe") if li_rc < 0 then destroy loo_Jwe MessageBox("Error","Connecting to COM object failed") return end if // First build the JWE Protected Header: {"enc":"A128CBC-HS256"} loo_JweProtHdr = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JweProtHdr.ConnectToNewObject("Chilkat.JsonObject") loo_JweProtHdr.AppendString("enc","A128CBC-HS256") loo_Jwe.SetProtectedHeader(loo_JweProtHdr) // We have a single recipient that uses AES Key Wrap to encrypt the CEK. // // {"alg":"A128KW","kid":"7"} loo_JweRecipientHdr = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JweRecipientHdr.ConnectToNewObject("Chilkat.JsonObject") loo_JweRecipientHdr.AppendString("alg","A128KW") loo_JweRecipientHdr.AppendString("kid","7") li_RecipientIndex = 0 loo_Jwe.SetRecipientHeader(li_RecipientIndex,loo_JweRecipientHdr) // Set the Shared Unprotected Header: {"jku":"https://server.example.com/keys.jwks"} loo_JweUnprotHdr = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JweUnprotHdr.ConnectToNewObject("Chilkat.JsonObject") loo_JweUnprotHdr.AppendString("jku","https://server.example.com/keys.jwks") loo_Jwe.SetUnprotectedHeader(loo_JweUnprotHdr) // Set the AES key wrapping key. ls_AesWrappingKey = "GawgguFyGrWKav7AX4VKUg" li_RecipientIndex = 0 loo_Jwe.SetWrappingKey(li_RecipientIndex,ls_AesWrappingKey,"base64url") // OK.. everything has been specified. // Now encrypt. // Indicate that we prefer the flattened JSON serialization. loo_Jwe.PreferFlattened = 1 ls_StrJwe = loo_Jwe.Encrypt(ls_Plaintext,"utf-8") if loo_Jwe.LastMethodSuccess <> 1 then Write-Debug loo_Jwe.LastErrorText destroy loo_Jwe destroy loo_JweProtHdr destroy loo_JweRecipientHdr destroy loo_JweUnprotHdr return end if // 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: loo_JsonTemp = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JsonTemp.ConnectToNewObject("Chilkat.JsonObject") loo_JsonTemp.Load(ls_StrJwe) loo_JsonTemp.EmitCompact = 0 Write-Debug loo_JsonTemp.Emit() // 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.. loo_Jwe2 = create oleobject // Use "Chilkat_9_5_0.Jwe" for versions of Chilkat < 10.0.0 li_rc = loo_Jwe2.ConnectToNewObject("Chilkat.Jwe") li_Success = loo_Jwe2.LoadJwe(ls_StrJwe) if li_Success <> 1 then Write-Debug loo_Jwe2.LastErrorText destroy loo_Jwe destroy loo_JweProtHdr destroy loo_JweRecipientHdr destroy loo_JweUnprotHdr destroy loo_JsonTemp destroy loo_Jwe2 return end if // Set the AES wrap key.. li_RecipientIndex = 0 loo_Jwe2.SetWrappingKey(li_RecipientIndex,ls_AesWrappingKey,"base64url") // Decrypt ls_OriginalPlaintext = loo_Jwe2.Decrypt(li_RecipientIndex,"utf-8") if loo_Jwe2.LastMethodSuccess <> 1 then Write-Debug loo_Jwe2.LastErrorText destroy loo_Jwe destroy loo_JweProtHdr destroy loo_JweRecipientHdr destroy loo_JweUnprotHdr destroy loo_JsonTemp destroy loo_Jwe2 return end if Write-Debug "original text decrypted with AES key wrap key: " Write-Debug ls_OriginalPlaintext // --------------------------------------------------------------------------------- // 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. loo_SbJwe = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbJwe.ConnectToNewObject("Chilkat.StringBuilder") loo_SbJwe.Append("{") loo_SbJwe.Append("~"protected~":") loo_SbJwe.Append("~"eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0~",") loo_SbJwe.Append("~"unprotected~":") loo_SbJwe.Append("{~"jku~":~"https://server.example.com/keys.jwks~"},") loo_SbJwe.Append("~"header~":") loo_SbJwe.Append("{~"alg~":~"A128KW~",~"kid~":~"7~"},") loo_SbJwe.Append("~"encrypted_key~":") loo_SbJwe.Append("~"6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ~",") loo_SbJwe.Append("~"iv~":") loo_SbJwe.Append("~"AxY8DCtDaGlsbGljb3RoZQ~",") loo_SbJwe.Append("~"ciphertext~":") loo_SbJwe.Append("~"KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY~",") loo_SbJwe.Append("~"tag~":") loo_SbJwe.Append("~"Mz-VPPyU4RlcuYv1IwIvzw~"") loo_SbJwe.Append("}") li_Success = loo_Jwe2.LoadJweSb(loo_SbJwe) if li_Success <> 1 then Write-Debug loo_Jwe2.LastErrorText destroy loo_Jwe destroy loo_JweProtHdr destroy loo_JweRecipientHdr destroy loo_JweUnprotHdr destroy loo_JsonTemp destroy loo_Jwe2 destroy loo_SbJwe return end if li_RecipientIndex = 0 loo_Jwe2.SetWrappingKey(li_RecipientIndex,ls_AesWrappingKey,"base64url") ls_OriginalPlaintext = loo_Jwe2.Decrypt(li_RecipientIndex,"utf-8") if loo_Jwe2.LastMethodSuccess <> 1 then Write-Debug loo_Jwe2.LastErrorText destroy loo_Jwe destroy loo_JweProtHdr destroy loo_JweRecipientHdr destroy loo_JweUnprotHdr destroy loo_JsonTemp destroy loo_Jwe2 destroy loo_SbJwe return end if Write-Debug "original text decrypted from published flattened JWE: " Write-Debug ls_OriginalPlaintext // 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. destroy loo_Jwe destroy loo_JweProtHdr destroy loo_JweRecipientHdr destroy loo_JweUnprotHdr destroy loo_JsonTemp destroy loo_Jwe2 destroy loo_SbJwe |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.