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
(Delphi DLL) 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
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, BinData, Task, Stream, Crypt2, Compression; ... procedure TForm1.Button1Click(Sender: TObject); var success: Boolean; compress: HCkCompression; crypt: HCkCrypt2; ivHex: PWideChar; keyHex: PWideChar; streamC: HCkStream; streamE: HCkStream; taskC: HCkTask; taskE: HCkTask; outData: HCkBinData; begin // 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) do begin if (CkStream_getDataAvailable(streamE) = True) then begin success := CkStream_ReadBd(streamE,outData); end; end; // Let's make sure the background task finished. // It should already be the case that the task is finished. while ((CkTask_getFinished(taskE) <> True) or (CkTask_getFinished(taskC) <> True)) do begin CkTask_SleepMs(taskE,20); end; // Get any remaining data that needs to be flushed. if (CkStream_getDataAvailable(streamE) = True) then begin success := CkStream_ReadBd(streamE,outData); end; // Double-check for success.. if (CkTask_getTaskSuccess(taskE) <> True) then begin Memo1.Lines.Add('async encryption failed:'); Memo1.Lines.Add(CkTask__resultErrorText(taskE)); success := False; end; // Double-check for success.. if (CkTask_getTaskSuccess(taskC) <> True) then begin Memo1.Lines.Add('async compression failed:'); Memo1.Lines.Add(CkTask__resultErrorText(taskC)); success := False; end; CkTask_Dispose(taskE); CkTask_Dispose(taskC); // Save the compressed/encrypted data to a file. CkBinData_WriteFile(outData,'qa_output/compressedEncrypted.dat'); Memo1.Lines.Add('The async compress/encrypt was successful.'); // 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); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.