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
(AutoIt) Streaming EncryptionEncrypt and decrypt using a stream.
; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. $oCrypt = ObjCreate("Chilkat.Crypt2") ; Setup encryption using the chacha20 algorithm... $oCrypt.CryptAlgorithm = "chacha20" $oCrypt.KeyLength = 256 $oCrypt.EncodingMode = "hex" Local $sIvHex = "000000000000000000000002" $oCrypt.SetEncodedIV $sIvHex,"hex" $oCrypt.InitialCount = 42 Local $sKeyHex = "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0" $oCrypt.SetEncodedKey $sKeyHex,"hex" Local $sPlainText = "The quick brown fox jumped over the lazy dog." & @CRLF $oStream = ObjCreate("Chilkat.Stream") ; We'll save the encrypted output in eStrings to demonstrate streaming decryption next. $oEStrings = ObjCreate("Chilkat.StringArray") ; Start a background task that will encrypt a stream. Local $oTask = $oCrypt.EncryptStreamAsync($oStream) Local $bSuccess = $oTask.Run() ; Write plainText to the stream, and read chacha20 encrypted text.. Local $sCipherText Local $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. $bSuccess = $oStream.WriteString($sPlainText) If ($oStream.DataAvailable = True) Then $sCipherText = $oStream.ReadBytesENC("hex") ConsoleWrite($sCipherText & @CRLF) $bSuccess = $oEStrings.Append($sCipherText) EndIf Next ; Tell the background task that the stream has ended. $bSuccess = $oStream.WriteClose() ; Let's make sure the background task finished. ; It should already be the case that the task is finished. While ($oTask.Finished <> True) $oTask.SleepMs 20 Wend ; Get any remaining data available from the stream. If ($oStream.DataAvailable = True) Then $sCipherText = $oStream.ReadBytesENC("hex") ConsoleWrite($sCipherText & @CRLF) $bSuccess = $oEStrings.Append($sCipherText) EndIf If ($oTask.TaskSuccess <> True) Then ConsoleWrite("async encryption failed:" & @CRLF) ConsoleWrite($oTask.ResultErrorText & @CRLF) $bSuccess = False EndIf ConsoleWrite("-- encrypt finished --" & @CRLF) ; Now decrypt to return the original. ; Reset the stream object so it can be used again. $oStream.Reset ; Start a background task that will decrypt a stream. $oTask = $oCrypt.DecryptStreamAsync($oStream) $bSuccess = $oTask.Run() Local $iN = $oEStrings.Count For $i = 0 To $iN - 1 $bSuccess = $oStream.WriteBytesENC($oEStrings.GetString($i),"hex") If ($oStream.DataAvailable = True) Then $sPlainText = $oStream.ReadString() ConsoleWrite($sPlainText & @CRLF) EndIf Next ; Tell the background task that the stream has ended. $bSuccess = $oStream.WriteClose() ; Let's make sure the background task finished. ; It should already be the case that the task is finished. While ($oTask.Finished <> True) $oTask.SleepMs 20 Wend ; Get any remaining data available from the stream. If ($oStream.DataAvailable = True) Then $sPlainText = $oStream.ReadString() ConsoleWrite($sPlainText & @CRLF) EndIf If ($oTask.TaskSuccess <> True) Then ConsoleWrite("async decryption failed:" & @CRLF) ConsoleWrite($oTask.ResultErrorText & @CRLF) $bSuccess = False EndIf ConsoleWrite("-- decrypt finished --" & @CRLF) |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.