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
(Tcl) 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
load ./chilkat.dll # This requires the Chilkat API to have been previously unlocked. # See Global Unlock Sample for sample code. # Initialize the compression object. set compress [new_CkCompression] CkCompression_put_Algorithm $compress "deflate" # Initialize the encryption object. set crypt [new_CkCrypt2] CkCrypt2_put_CryptAlgorithm $crypt "chacha20" CkCrypt2_put_KeyLength $crypt 256 CkCrypt2_put_EncodingMode $crypt "hex" set ivHex "000000000000000000000002" CkCrypt2_SetEncodedIV $crypt $ivHex "hex" CkCrypt2_put_InitialCount $crypt 42 set keyHex "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0" CkCrypt2_SetEncodedKey $crypt $keyHex "hex" # Setup the streams. # streamC is for compression set streamC [new_CkStream] # streamE is for encryption set streamE [new_CkStream] # The source for the compressor is a file. CkStream_put_SourceFile $streamC "qa_data/hamlet.xml" # The source for the encryptor is the output of the compressor stream. set success [CkStream_SetSourceStream $streamE $streamC] # Our application will be reading the output of streamE. # Start the compression and encryption streams in background threads.. # taskC is a CkTask set taskC [CkCompression_CompressStreamAsync $compress $streamC] # taskE is a CkTask set taskE [CkCrypt2_EncryptStreamAsync $crypt $streamE] set success [CkTask_Run $taskC] set success [CkTask_Run $taskE] # Read the compressed and encrypted bytes. set outData [new_CkBinData] while {[CkStream_get_EndOfStream $streamE] != 1} { if {[CkStream_get_DataAvailable $streamE] == 1} then { set 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 {expr [[CkTask_get_Finished $taskE] != 1] || [[CkTask_get_Finished $taskC] != 1]} { CkTask_SleepMs $taskE 20 } # Get any remaining data that needs to be flushed. if {[CkStream_get_DataAvailable $streamE] == 1} then { set success [CkStream_ReadBd $streamE $outData] } # Double-check for success.. if {[CkTask_get_TaskSuccess $taskE] != 1} then { puts "async encryption failed:" puts [CkTask_resultErrorText $taskE] set success 0 } # Double-check for success.. if {[CkTask_get_TaskSuccess $taskC] != 1} then { puts "async compression failed:" puts [CkTask_resultErrorText $taskC] set success 0 } delete_CkTask $taskE delete_CkTask $taskC # Save the compressed/encrypted data to a file. CkBinData_WriteFile $outData "qa_output/compressedEncrypted.dat" puts "The async compress/encrypt was successful." # Let's decrypt and decompress to further verify... set success [CkCrypt2_DecryptBd $crypt $outData] set success [CkCompression_DecompressBd $compress $outData] # outData should contain the original data.. set success [CkBinData_WriteFile $outData "qa_output/original_hamlet.xml"] delete_CkCompression $compress delete_CkCrypt2 $crypt delete_CkStream $streamC delete_CkStream $streamE delete_CkBinData $outData |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.