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
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
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) Streaming Compression --> Streaming Encryption

Demonstrate how to feed a compression stream into an encryption stream. This example does the following:

Data File --> Compress --> Encrypt --> Application Reads

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCompress
    Handle hoCrypt
    String sIvHex
    String sKeyHex
    Variant vStreamC
    Handle hoStreamC
    Variant vStreamE
    Handle hoStreamE
    Variant vTaskC
    Handle hoTaskC
    Variant vTaskE
    Handle hoTaskE
    Variant vOutData
    Handle hoOutData
    String sTemp1
    Boolean bTemp1

    //  This requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  Initialize the compression object.
    Get Create (RefClass(cComChilkatCompression)) To hoCompress
    If (Not(IsComObjectCreated(hoCompress))) Begin
        Send CreateComObject of hoCompress
    End
    Set ComAlgorithm Of hoCompress To "deflate"

    //  Initialize the encryption object.
    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Set ComCryptAlgorithm Of hoCrypt To "chacha20"
    Set ComKeyLength Of hoCrypt To 256
    Set ComEncodingMode Of hoCrypt To "hex"
    Move "000000000000000000000002" To sIvHex
    Send ComSetEncodedIV To hoCrypt sIvHex "hex"
    Set ComInitialCount Of hoCrypt To 42
    Move "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0" To sKeyHex
    Send ComSetEncodedKey To hoCrypt sKeyHex "hex"

    //  Setup the streams.
    //  streamC is for compression
    Get Create (RefClass(cComChilkatStream)) To hoStreamC
    If (Not(IsComObjectCreated(hoStreamC))) Begin
        Send CreateComObject of hoStreamC
    End
    //  streamE is for encryption
    Get Create (RefClass(cComChilkatStream)) To hoStreamE
    If (Not(IsComObjectCreated(hoStreamE))) Begin
        Send CreateComObject of hoStreamE
    End

    //  The source for the compressor is a file.
    Set ComSourceFile Of hoStreamC To "qa_data/hamlet.xml"
    //  The source for the encryptor is the output of the compressor stream.
    Get pvComObject of hoStreamC to vStreamC
    Get ComSetSourceStream Of hoStreamE vStreamC To iSuccess

    //  Our application will be reading the output of streamE.
    //  Start the compression and encryption streams in background threads..
    Get pvComObject of hoStreamC to vStreamC
    Get ComCompressStreamAsync Of hoCompress vStreamC To vTaskC
    If (IsComObject(vTaskC)) Begin
        Get Create (RefClass(cComChilkatTask)) To hoTaskC
        Set pvComObject Of hoTaskC To vTaskC
    End
    Get pvComObject of hoStreamE to vStreamE
    Get ComEncryptStreamAsync Of hoCrypt vStreamE To vTaskE
    If (IsComObject(vTaskE)) Begin
        Get Create (RefClass(cComChilkatTask)) To hoTaskE
        Set pvComObject Of hoTaskE To vTaskE
    End
    Get ComRun Of hoTaskC To iSuccess
    Get ComRun Of hoTaskE To iSuccess

    //  Read the compressed and encrypted bytes.
    Get Create (RefClass(cComChilkatBinData)) To hoOutData
    If (Not(IsComObjectCreated(hoOutData))) Begin
        Send CreateComObject of hoOutData
    End
    While ((ComEndOfStream(hoStreamE)) <> True)
        Get ComDataAvailable Of hoStreamE To bTemp1
        If (bTemp1 = True) Begin
            Get pvComObject of hoOutData to vOutData
            Get ComReadBd Of hoStreamE vOutData To iSuccess
        End

    Loop

    //  Let's make sure the background task finished.
    //  It should already be the case that the task is finished.
    While (((ComFinished(hoTaskE)) <> True) Or ((ComFinished(hoTaskC)) <> True))
        Send ComSleepMs To hoTaskE 20
    Loop

    //  Get any remaining data that needs to be flushed.
    Get ComDataAvailable Of hoStreamE To bTemp1
    If (bTemp1 = True) Begin
        Get pvComObject of hoOutData to vOutData
        Get ComReadBd Of hoStreamE vOutData To iSuccess
    End

    //  Double-check for success..
    Get ComTaskSuccess Of hoTaskE To bTemp1
    If (bTemp1 <> True) Begin
        Showln "async encryption failed:"
        Get ComResultErrorText Of hoTaskE To sTemp1
        Showln sTemp1
        Move False To iSuccess
    End

    //  Double-check for success..
    Get ComTaskSuccess Of hoTaskC To bTemp1
    If (bTemp1 <> True) Begin
        Showln "async compression failed:"
        Get ComResultErrorText Of hoTaskC To sTemp1
        Showln sTemp1
        Move False To iSuccess
    End

    Send Destroy of hoTaskE
    Send Destroy of hoTaskC

    //  Save the compressed/encrypted data to a file.
    Get ComWriteFile Of hoOutData "qa_output/compressedEncrypted.dat" To iSuccess

    Showln "The async compress/encrypt was successful."

    //  Let's decrypt and decompress to further verify...
    Get pvComObject of hoOutData to vOutData
    Get ComDecryptBd Of hoCrypt vOutData To iSuccess
    Get pvComObject of hoOutData to vOutData
    Get ComDecompressBd Of hoCompress vOutData To iSuccess

    //  outData should contain the original data..
    Get ComWriteFile Of hoOutData "qa_output/original_hamlet.xml" To iSuccess


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.