Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) JWE with DEFLATE Compression

Demonstrates how to DEFLATE ("zip") compress the JWE payload prior to encryption.

Note: This example requires Chilkat v9.5.0.66 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

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.