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
(PureBasic) 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
IncludeFile "CkTask.pb" IncludeFile "CkBinData.pb" IncludeFile "CkCompression.pb" IncludeFile "CkStream.pb" IncludeFile "CkCrypt2.pb" Procedure ChilkatExample() ; This requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. success.i ; Initialize the compression object. compress.i = CkCompression::ckCreate() If compress.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkCompression::setCkAlgorithm(compress, "deflate") ; Initialize the encryption object. crypt.i = CkCrypt2::ckCreate() If crypt.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkCrypt2::setCkCryptAlgorithm(crypt, "chacha20") CkCrypt2::setCkKeyLength(crypt, 256) CkCrypt2::setCkEncodingMode(crypt, "hex") ivHex.s = "000000000000000000000002" CkCrypt2::ckSetEncodedIV(crypt,ivHex,"hex") CkCrypt2::setCkInitialCount(crypt, 42) keyHex.s = "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0" CkCrypt2::ckSetEncodedKey(crypt,keyHex,"hex") ; Setup the streams. ; streamC is for compression streamC.i = CkStream::ckCreate() If streamC.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; streamE is for encryption streamE.i = CkStream::ckCreate() If streamE.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; The source for the compressor is a file. CkStream::setCkSourceFile(streamC, "qa_data/hamlet.xml") ; The source for the encryptor is the output of the compressor stream. success = CkStream::ckSetSourceStream(streamE,streamC) ; Our application will be reading the output of streamE. ; Start the compression and encryption streams in background threads.. taskC.i = CkCompression::ckCompressStreamAsync(compress,streamC) taskE.i = CkCrypt2::ckEncryptStreamAsync(crypt,streamE) success = CkTask::ckRun(taskC) success = CkTask::ckRun(taskE) ; Read the compressed and encrypted bytes. outData.i = CkBinData::ckCreate() If outData.i = 0 Debug "Failed to create object." ProcedureReturn EndIf While (CkStream::ckEndOfStream(streamE) <> 1) If CkStream::ckDataAvailable(streamE) = 1 success = CkStream::ckReadBd(streamE,outData) EndIf Wend ; Let's make sure the background task finished. ; It should already be the case that the task is finished. While ((CkTask::ckFinished(taskE) <> 1) OR (CkTask::ckFinished(taskC) <> 1)) CkTask::ckSleepMs(taskE,20) Wend ; Get any remaining data that needs to be flushed. If CkStream::ckDataAvailable(streamE) = 1 success = CkStream::ckReadBd(streamE,outData) EndIf ; Double-check for success.. If CkTask::ckTaskSuccess(taskE) <> 1 Debug "async encryption failed:" Debug CkTask::ckResultErrorText(taskE) success = 0 EndIf ; Double-check for success.. If CkTask::ckTaskSuccess(taskC) <> 1 Debug "async compression failed:" Debug CkTask::ckResultErrorText(taskC) success = 0 EndIf CkTask::ckDispose(taskE) CkTask::ckDispose(taskC) ; Save the compressed/encrypted data to a file. CkBinData::ckWriteFile(outData,"qa_output/compressedEncrypted.dat") Debug "The async compress/encrypt was successful." ; Let's decrypt and decompress to further verify... success = CkCrypt2::ckDecryptBd(crypt,outData) success = CkCompression::ckDecompressBd(compress,outData) ; outData should contain the original data.. success = CkBinData::ckWriteFile(outData,"qa_output/original_hamlet.xml") CkCompression::ckDispose(compress) CkCrypt2::ckDispose(crypt) CkStream::ckDispose(streamC) CkStream::ckDispose(streamE) CkBinData::ckDispose(outData) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.