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
(DataFlex) JWE with DEFLATE CompressionDemonstrates how to DEFLATE ("zip") compress the JWE payload prior to encryption. Note: This example requires Chilkat v9.5.0.66 or greater.
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess Variant vSbPlainText Handle hoSbPlainText Boolean iBCrLf String sLine Handle hoJwe ProtHdr Handle hoJweProtHdr String sAesWrappingKey Variant vSbJweCompressed Handle hoSbJweCompressed Variant vSbJweUncompressed Handle hoSbJweUncompressed 2 Handle hoJwe2 Variant vSbOriginalText Handle hoSbOriginalText String sTemp1 Integer iTemp1 // 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. // Create some plaintext to be encrypted. // This example will demonstrate with and without DEFLATE (zip) compression. Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPlainText If (Not(IsComObjectCreated(hoSbPlainText))) Begin Send CreateComObject of hoSbPlainText End Move True To iBCrLf Move "Live long and prosper." To sLine Get ComAppendLine Of hoSbPlainText sLine iBCrLf To iSuccess Get ComAppendLine Of hoSbPlainText sLine iBCrLf To iSuccess Get ComAppendLine Of hoSbPlainText sLine iBCrLf To iSuccess Get ComAppendLine Of hoSbPlainText sLine iBCrLf To iSuccess // The text to be encrypted: Get ComGetAsString Of hoSbPlainText To sTemp1 Showln sTemp1 Get Create (RefClass(cComChilkatJwe)) To hoJwe If (Not(IsComObjectCreated(hoJwe))) Begin Send CreateComObject of hoJwe End // Build the JWE Protected Header: {"alg":"A128KW","enc":"A128CBC-HS256","zip":"DEF"} // The "zip":"DEF" parameter indicates that the plaintext payload should // be compressed prior to encryption. Get Create (RefClass(cComChilkatJsonObject)) To hoJweProtHdr If (Not(IsComObjectCreated(hoJweProtHdr))) Begin Send CreateComObject of hoJweProtHdr End Get ComAppendString Of hoJweProtHdr "alg" "A128KW" To iSuccess Get ComAppendString Of hoJweProtHdr "enc" "A128CBC-HS256" To iSuccess Get ComAppendString Of hoJweProtHdr "zip" "DEF" To iSuccess Get pvComObject of hoJweProtHdr to vJweProtHdr Get ComSetProtectedHeader Of hoJwe vJweProtHdr To iSuccess // Set the AES key wrap key: Move "GawgguFyGrWKav7AX4VKUg" To sAesWrappingKey Get ComSetWrappingKey Of hoJwe 0 sAesWrappingKey "base64url" To iSuccess // Encrypt and return the JWE in sbJweCompressed: Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJweCompressed If (Not(IsComObjectCreated(hoSbJweCompressed))) Begin Send CreateComObject of hoSbJweCompressed End Get pvComObject of hoSbPlainText to vSbPlainText Get pvComObject of hoSbJweCompressed to vSbJweCompressed Get ComEncryptSb Of hoJwe vSbPlainText "utf-8" vSbJweCompressed To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoJwe To sTemp1 Showln sTemp1 Procedure_Return End // Show the compressed JWE: Get ComGetAsString Of hoSbJweCompressed To sTemp1 Showln sTemp1 Get ComLength Of hoSbJweCompressed To iTemp1 Showln "size of compressed JWE: " iTemp1 // Now create a JWE without compression. Get ComDelete Of hoJweProtHdr "zip" To iSuccess // Make sure to update the shared protected header: Get pvComObject of hoJweProtHdr to vJweProtHdr Get ComSetProtectedHeader Of hoJwe vJweProtHdr To iSuccess Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJweUncompressed If (Not(IsComObjectCreated(hoSbJweUncompressed))) Begin Send CreateComObject of hoSbJweUncompressed End Get pvComObject of hoSbPlainText to vSbPlainText Get pvComObject of hoSbJweUncompressed to vSbJweUncompressed Get ComEncryptSb Of hoJwe vSbPlainText "utf-8" vSbJweUncompressed To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoJwe To sTemp1 Showln sTemp1 Procedure_Return End // Show the uncompressed JWE: Get ComGetAsString Of hoSbJweUncompressed To sTemp1 Showln sTemp1 Get ComLength Of hoSbJweUncompressed To iTemp1 Showln "size of uncompressed JWE: " iTemp1 // Decrypting is the same whether compression is used or not. // The "zip" header in the JWE indicates that the payload should be // automatically decompressed (inflated) after decrypting. Get Create (RefClass(cComChilkatJwe)) To hoJwe2 If (Not(IsComObjectCreated(hoJwe2))) Begin Send CreateComObject of hoJwe2 End Get pvComObject of hoSbJweCompressed to vSbJweCompressed Get ComLoadJweSb Of hoJwe2 vSbJweCompressed To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoJwe2 To sTemp1 Showln sTemp1 Procedure_Return End // Set the AES wrap key. Get ComSetWrappingKey Of hoJwe2 0 sAesWrappingKey "base64url" To iSuccess // Decrypt (also automatically decompresses). Get Create (RefClass(cComChilkatStringBuilder)) To hoSbOriginalText If (Not(IsComObjectCreated(hoSbOriginalText))) Begin Send CreateComObject of hoSbOriginalText End Get pvComObject of hoSbOriginalText to vSbOriginalText Get ComDecryptSb Of hoJwe2 0 "utf-8" vSbOriginalText To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoJwe2 To sTemp1 Showln sTemp1 Procedure_Return End Showln "original text from compressed JWE: " Get ComGetAsString Of hoSbOriginalText To sTemp1 Showln sTemp1 // ----------------------------------------------------------- // Do the same with the uncompressed JWE Get pvComObject of hoSbJweUncompressed to vSbJweUncompressed Get ComLoadJweSb Of hoJwe2 vSbJweUncompressed To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoJwe2 To sTemp1 Showln sTemp1 Procedure_Return End // Set the AES wrap key. Get ComSetWrappingKey Of hoJwe2 0 sAesWrappingKey "base64url" To iSuccess // Decrypt. Send ComClear To hoSbOriginalText Get pvComObject of hoSbOriginalText to vSbOriginalText Get ComDecryptSb Of hoJwe2 0 "utf-8" vSbOriginalText To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoJwe2 To sTemp1 Showln sTemp1 Procedure_Return End Showln "original text from uncompressed JWE: " Get ComGetAsString Of hoSbOriginalText To sTemp1 Showln sTemp1 // ------------------------------------------------ // The output of this example is: // (Note: Your output data will be different because the content encryption key is randomly generated.) // eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiemlwIjoiREVGIn0.xuW-pEAIdEUFnk10m8ocursvktO8Of9ByCCAt6LgKkkOtCWCUn1kQw.zpGj-9WVni3cQxyOuZbcGA.0hzP1myua3oYpUHwCIY_3edBUREbUpLaX6wYuJduOdI.Ppc6aEO3y3B8BJ1FKMPjlA // size of compressed JWE: 212 // eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.N4KeyC7nnSFkieJOyE24_zKeuV_m7v5UKoJb1TgV4Yc_r2RzUPNvyA.6AEdyXSCKx-iMmUJyypSLg.QpixfyrwhGpmwUDp623viik4smPav7vwPLiC2r-V-jwnSfEH3mxWu6DbrIz3mixaqATwynmEBzVPxvS9jTXpSAGCnniib4_0WoPl3r_wF5tlsKOEe--jpNso-DKd1Tp8jJxj3JkFWt3IRnUUKGj17g.sBfDwFc5fzpaI-UW8-SW4g // size of uncompressed JWE: 303 // original text from compressed JWE: // Live long and prosper. // Live long and prosper. // Live long and prosper. // Live long and prosper. // // original text from uncompressed JWE: // Live long and prosper. // Live long and prosper. // Live long and prosper. // Live long and prosper. // End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.