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) Streaming Compression --> Streaming EncryptionDemonstrate how to feed a compression stream into an encryption stream. This example does the following: Data File --> Compress --> Encrypt --> Application Reads
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.