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 EncryptionEncrypt and decrypt using a stream.
IncludeFile "CkTask.pb" IncludeFile "CkStream.pb" IncludeFile "CkStringArray.pb" IncludeFile "CkCrypt2.pb" Procedure ChilkatExample() ; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. crypt.i = CkCrypt2::ckCreate() If crypt.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Setup encryption using the chacha20 algorithm... 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") plainText.s = "The quick brown fox jumped over the lazy dog." + Chr(13) + Chr(10) stream.i = CkStream::ckCreate() If stream.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; We'll save the encrypted output in eStrings to demonstrate streaming decryption next. eStrings.i = CkStringArray::ckCreate() If eStrings.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Start a background task that will encrypt a stream. task.i = CkCrypt2::ckEncryptStreamAsync(crypt,stream) success.i = CkTask::ckRun(task) ; Write plainText to the stream, and read chacha20 encrypted text.. cipherText.s i.i For i = 1 To 10 ; Note: An encryption algorithm's block size will cause buffering, ; and therefore not every loop iteration will produce output. success = CkStream::ckWriteString(stream,plainText) If CkStream::ckDataAvailable(stream) = 1 cipherText = CkStream::ckReadBytesENC(stream,"hex") Debug cipherText success = CkStringArray::ckAppend(eStrings,cipherText) EndIf Next ; Tell the background task that the stream has ended. success = CkStream::ckWriteClose(stream) ; Let's make sure the background task finished. ; It should already be the case that the task is finished. While (CkTask::ckFinished(task) <> 1) CkTask::ckSleepMs(task,20) Wend ; Get any remaining data available from the stream. If CkStream::ckDataAvailable(stream) = 1 cipherText = CkStream::ckReadBytesENC(stream,"hex") Debug cipherText success = CkStringArray::ckAppend(eStrings,cipherText) EndIf If CkTask::ckTaskSuccess(task) <> 1 Debug "async encryption failed:" Debug CkTask::ckResultErrorText(task) success = 0 EndIf CkTask::ckDispose(task) Debug "-- encrypt finished --" ; Now decrypt to return the original. ; Reset the stream object so it can be used again. CkStream::ckReset(stream) ; Start a background task that will decrypt a stream. task = CkCrypt2::ckDecryptStreamAsync(crypt,stream) success = CkTask::ckRun(task) n.i = CkStringArray::ckCount(eStrings) For i = 0 To n - 1 success = CkStream::ckWriteBytesENC(stream,CkStringArray::ckGetString(eStrings,i),"hex") If CkStream::ckDataAvailable(stream) = 1 plainText = CkStream::ckReadString(stream) Debug plainText EndIf Next ; Tell the background task that the stream has ended. success = CkStream::ckWriteClose(stream) ; Let's make sure the background task finished. ; It should already be the case that the task is finished. While (CkTask::ckFinished(task) <> 1) CkTask::ckSleepMs(task,20) Wend ; Get any remaining data available from the stream. If CkStream::ckDataAvailable(stream) = 1 plainText = CkStream::ckReadString(stream) Debug plainText EndIf If CkTask::ckTaskSuccess(task) <> 1 Debug "async decryption failed:" Debug CkTask::ckResultErrorText(task) success = 0 EndIf CkTask::ckDispose(task) Debug "-- decrypt finished --" CkCrypt2::ckDispose(crypt) CkStream::ckDispose(stream) CkStringArray::ckDispose(eStrings) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.