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
(Lianja) 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
// This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Initialize the compression object. loCompress = createobject("CkCompression") loCompress.Algorithm = "deflate" // Initialize the encryption object. loCrypt = createobject("CkCrypt2") loCrypt.CryptAlgorithm = "chacha20" loCrypt.KeyLength = 256 loCrypt.EncodingMode = "hex" lcIvHex = "000000000000000000000002" loCrypt.SetEncodedIV(lcIvHex,"hex") loCrypt.InitialCount = 42 lcKeyHex = "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0" loCrypt.SetEncodedKey(lcKeyHex,"hex") // Setup the streams. // streamC is for compression loStreamC = createobject("CkStream") // streamE is for encryption loStreamE = createobject("CkStream") // The source for the compressor is a file. loStreamC.SourceFile = "qa_data/hamlet.xml" // The source for the encryptor is the output of the compressor stream. llSuccess = loStreamE.SetSourceStream(loStreamC) // Our application will be reading the output of streamE. // Start the compression and encryption streams in background threads.. loTaskC = loCompress.CompressStreamAsync(loStreamC) loTaskE = loCrypt.EncryptStreamAsync(loStreamE) llSuccess = loTaskC.Run() llSuccess = loTaskE.Run() // Read the compressed and encrypted bytes. loOutData = createobject("CkBinData") do while (loStreamE.EndOfStream <> .T.) if (loStreamE.DataAvailable = .T.) then llSuccess = loStreamE.ReadBd(loOutData) endif enddo // Let's make sure the background task finished. // It should already be the case that the task is finished. do while ((loTaskE.Finished <> .T.) or (loTaskC.Finished <> .T.)) loTaskE.SleepMs(20) enddo // Get any remaining data that needs to be flushed. if (loStreamE.DataAvailable = .T.) then llSuccess = loStreamE.ReadBd(loOutData) endif // Double-check for success.. if (loTaskE.TaskSuccess <> .T.) then ? "async encryption failed:" ? loTaskE.ResultErrorText llSuccess = .F. endif // Double-check for success.. if (loTaskC.TaskSuccess <> .T.) then ? "async compression failed:" ? loTaskC.ResultErrorText llSuccess = .F. endif release loTaskE release loTaskC // Save the compressed/encrypted data to a file. loOutData.WriteFile("qa_output/compressedEncrypted.dat") ? "The async compress/encrypt was successful." // Let's decrypt and decompress to further verify... llSuccess = loCrypt.DecryptBd(loOutData) llSuccess = loCompress.DecompressBd(loOutData) // outData should contain the original data.. llSuccess = loOutData.WriteFile("qa_output/original_hamlet.xml") release loCompress release loCrypt release loStreamC release loStreamE release loOutData |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.