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) Stream a file to compression to encryption to an output file.Runs a chain of streams to read a source file, compress, encrypt, and write an output file.
#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 binData; // 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); // The sink for the encryptor is the output file. CkStream_putSinkFile(streamE,"qa_output/compressed_encrypted.dat"); // 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); // Let the streams run until finished. // 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); } // check for success.. if (CkTask_getTaskSuccess(taskE) != TRUE) { printf("async encryption failed:\n"); printf("%s\n",CkTask_resultErrorText(taskE)); success = FALSE; } // 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); // Load the file that was produced and decrypt/decompress to verify. binData = CkBinData_Create(); success = CkBinData_LoadFile(binData,"qa_output/compressed_encrypted.dat"); success = CkCrypt2_DecryptBd(crypt,binData); success = CkCompression_DecompressBd(compress,binData); // binData should contain the original data.. success = CkBinData_WriteFile(binData,"qa_output/original_hamlet.xml"); printf("Finished.\n"); CkCompression_Dispose(compress); CkCrypt2_Dispose(crypt); CkStream_Dispose(streamC); CkStream_Dispose(streamE); CkBinData_Dispose(binData); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.