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
(Visual FoxPro) 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
LOCAL lnSuccess LOCAL loCompress LOCAL loCrypt LOCAL lcIvHex LOCAL lcKeyHex LOCAL loStreamC LOCAL loStreamE LOCAL loTaskC LOCAL loTaskE LOCAL loOutData * This requires the Chilkat API to have been previously unlocked. * See Global Unlock Sample for sample code. * Initialize the compression object. loCompress = CreateObject('Chilkat_9_5_0.Compression') loCompress.Algorithm = "deflate" * Initialize the encryption object. loCrypt = CreateObject('Chilkat_9_5_0.Crypt2') 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('Chilkat_9_5_0.Stream') * streamE is for encryption loStreamE = CreateObject('Chilkat_9_5_0.Stream') * 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. lnSuccess = 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) lnSuccess = loTaskC.Run() lnSuccess = loTaskE.Run() * Read the compressed and encrypted bytes. loOutData = CreateObject('Chilkat_9_5_0.BinData') DO WHILE (loStreamE.EndOfStream <> 1) IF (loStreamE.DataAvailable = 1) THEN lnSuccess = 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 <> 1) OR (loTaskC.Finished <> 1)) loTaskE.SleepMs(20) ENDDO * Get any remaining data that needs to be flushed. IF (loStreamE.DataAvailable = 1) THEN lnSuccess = loStreamE.ReadBd(loOutData) ENDIF * Double-check for success.. IF (loTaskE.TaskSuccess <> 1) THEN ? "async encryption failed:" ? loTaskE.ResultErrorText lnSuccess = 0 ENDIF * Double-check for success.. IF (loTaskC.TaskSuccess <> 1) THEN ? "async compression failed:" ? loTaskC.ResultErrorText lnSuccess = 0 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... lnSuccess = loCrypt.DecryptBd(loOutData) lnSuccess = loCompress.DecompressBd(loOutData) * outData should contain the original data.. lnSuccess = 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.