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
(C) 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
#include <C_CkCompression.h> #include <C_CkCrypt2.h> #include <C_CkStream.h> #include <C_CkTask.h> #include <C_CkBinData.h> void ChilkatSample(void) { BOOL success; HCkCompression compress; HCkCrypt2 crypt; const char *ivHex; const char *keyHex; HCkStream streamC; HCkStream streamE; HCkTask taskC; HCkTask taskE; HCkBinData outData; // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Initialize the compression object. compress = CkCompression_Create(); CkCompression_putAlgorithm(compress,"deflate"); // Initialize the encryption object. crypt = CkCrypt2_Create(); CkCrypt2_putCryptAlgorithm(crypt,"chacha20"); CkCrypt2_putKeyLength(crypt,256); CkCrypt2_putEncodingMode(crypt,"hex"); ivHex = "000000000000000000000002"; CkCrypt2_SetEncodedIV(crypt,ivHex,"hex"); CkCrypt2_putInitialCount(crypt,42); keyHex = "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0"; CkCrypt2_SetEncodedKey(crypt,keyHex,"hex"); // Setup the streams. // streamC is for compression streamC = CkStream_Create(); // streamE is for encryption streamE = CkStream_Create(); // The source for the compressor is a file. CkStream_putSourceFile(streamC,"qa_data/hamlet.xml"); // The source for the encryptor is the output of the compressor stream. success = CkStream_SetSourceStream(streamE,streamC); // Our application will be reading the output of streamE. // Start the compression and encryption streams in background threads.. taskC = CkCompression_CompressStreamAsync(compress,streamC); taskE = CkCrypt2_EncryptStreamAsync(crypt,streamE); success = CkTask_Run(taskC); success = CkTask_Run(taskE); // Read the compressed and encrypted bytes. outData = CkBinData_Create(); while ((CkStream_getEndOfStream(streamE) != TRUE)) { if (CkStream_getDataAvailable(streamE) == TRUE) { success = CkStream_ReadBd(streamE,outData); } } // Let's make sure the background task finished. // It should already be the case that the task is finished. while (((CkTask_getFinished(taskE) != TRUE) || (CkTask_getFinished(taskC) != TRUE))) { CkTask_SleepMs(taskE,20); } // Get any remaining data that needs to be flushed. if (CkStream_getDataAvailable(streamE) == TRUE) { success = CkStream_ReadBd(streamE,outData); } // Double-check for success.. if (CkTask_getTaskSuccess(taskE) != TRUE) { printf("async encryption failed:\n"); printf("%s\n",CkTask_resultErrorText(taskE)); success = FALSE; } // Double-check for success.. if (CkTask_getTaskSuccess(taskC) != TRUE) { printf("async compression failed:\n"); printf("%s\n",CkTask_resultErrorText(taskC)); success = FALSE; } CkTask_Dispose(taskE); CkTask_Dispose(taskC); // Save the compressed/encrypted data to a file. CkBinData_WriteFile(outData,"qa_output/compressedEncrypted.dat"); printf("The async compress/encrypt was successful.\n"); // Let's decrypt and decompress to further verify... success = CkCrypt2_DecryptBd(crypt,outData); success = CkCompression_DecompressBd(compress,outData); // outData should contain the original data.. success = CkBinData_WriteFile(outData,"qa_output/original_hamlet.xml"); CkCompression_Dispose(compress); CkCrypt2_Dispose(crypt); CkStream_Dispose(streamC); CkStream_Dispose(streamE); CkBinData_Dispose(outData); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.