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
(Tcl) Streaming EncryptionEncrypt and decrypt using a stream.
load ./chilkat.dll # This example assumes the Chilkat API to have been previously unlocked. # See Global Unlock Sample for sample code. set crypt [new_CkCrypt2] # Setup encryption using the chacha20 algorithm... CkCrypt2_put_CryptAlgorithm $crypt "chacha20" CkCrypt2_put_KeyLength $crypt 256 CkCrypt2_put_EncodingMode $crypt "hex" set ivHex "000000000000000000000002" CkCrypt2_SetEncodedIV $crypt $ivHex "hex" CkCrypt2_put_InitialCount $crypt 42 set keyHex "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0" CkCrypt2_SetEncodedKey $crypt $keyHex "hex" set plainText "The quick brown fox jumped over the lazy dog.\r\n" set stream [new_CkStream] # We'll save the encrypted output in eStrings to demonstrate streaming decryption next. set eStrings [new_CkStringArray] # Start a background task that will encrypt a stream. # task is a CkTask set task [CkCrypt2_EncryptStreamAsync $crypt $stream] set success [CkTask_Run $task] # Write plainText to the stream, and read chacha20 encrypted text.. for {set i 1} {$i <= 10} {incr i} { # Note: An encryption algorithm's block size will cause buffering, # and therefore not every loop iteration will produce output. set success [CkStream_WriteString $stream $plainText] if {[CkStream_get_DataAvailable $stream] == 1} then { set cipherText [CkStream_readBytesENC $stream "hex"] puts "$cipherText" set success [CkStringArray_Append $eStrings $cipherText] } } # Tell the background task that the stream has ended. set success [CkStream_WriteClose $stream] # Let's make sure the background task finished. # It should already be the case that the task is finished. while {[CkTask_get_Finished $task] != 1} { CkTask_SleepMs $task 20 } # Get any remaining data available from the stream. if {[CkStream_get_DataAvailable $stream] == 1} then { set cipherText [CkStream_readBytesENC $stream "hex"] puts "$cipherText" set success [CkStringArray_Append $eStrings $cipherText] } if {[CkTask_get_TaskSuccess $task] != 1} then { puts "async encryption failed:" puts [CkTask_resultErrorText $task] set success 0 } delete_CkTask $task puts "-- encrypt finished --" # Now decrypt to return the original. # Reset the stream object so it can be used again. CkStream_Reset $stream # Start a background task that will decrypt a stream. set task [CkCrypt2_DecryptStreamAsync $crypt $stream] set success [CkTask_Run $task] set n [CkStringArray_get_Count $eStrings] for {set i 0} {$i <= [expr $n - 1]} {incr i} { set success [CkStream_WriteBytesENC $stream [CkStringArray_getString $eStrings $i] "hex"] if {[CkStream_get_DataAvailable $stream] == 1} then { set plainText [CkStream_readString $stream] puts "$plainText" } } # Tell the background task that the stream has ended. set success [CkStream_WriteClose $stream] # Let's make sure the background task finished. # It should already be the case that the task is finished. while {[CkTask_get_Finished $task] != 1} { CkTask_SleepMs $task 20 } # Get any remaining data available from the stream. if {[CkStream_get_DataAvailable $stream] == 1} then { set plainText [CkStream_readString $stream] puts "$plainText" } if {[CkTask_get_TaskSuccess $task] != 1} then { puts "async decryption failed:" puts [CkTask_resultErrorText $task] set success 0 } delete_CkTask $task puts "-- decrypt finished --" delete_CkCrypt2 $crypt delete_CkStream $stream delete_CkStringArray $eStrings |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.