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
(PowerBuilder) Streaming EncryptionEncrypt and decrypt using a stream.
integer li_rc oleobject loo_Crypt string ls_IvHex string ls_KeyHex string ls_PlainText oleobject loo_Stream oleobject loo_EStrings oleobject loo_Task integer li_Success string ls_CipherText integer i integer n // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Crypt = create oleobject // Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2") if li_rc < 0 then destroy loo_Crypt MessageBox("Error","Connecting to COM object failed") return end if // Setup encryption using the chacha20 algorithm... loo_Crypt.CryptAlgorithm = "chacha20" loo_Crypt.KeyLength = 256 loo_Crypt.EncodingMode = "hex" ls_IvHex = "000000000000000000000002" loo_Crypt.SetEncodedIV(ls_IvHex,"hex") loo_Crypt.InitialCount = 42 ls_KeyHex = "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0" loo_Crypt.SetEncodedKey(ls_KeyHex,"hex") ls_PlainText = "The quick brown fox jumped over the lazy dog.~r~n" loo_Stream = create oleobject // Use "Chilkat_9_5_0.Stream" for versions of Chilkat < 10.0.0 li_rc = loo_Stream.ConnectToNewObject("Chilkat.Stream") // We'll save the encrypted output in eStrings to demonstrate streaming decryption next. loo_EStrings = create oleobject // Use "Chilkat_9_5_0.StringArray" for versions of Chilkat < 10.0.0 li_rc = loo_EStrings.ConnectToNewObject("Chilkat.StringArray") // Start a background task that will encrypt a stream. loo_Task = loo_Crypt.EncryptStreamAsync(loo_Stream) li_Success = loo_Task.Run() // Write plainText to the stream, and read chacha20 encrypted text.. for i = 1 to 10 // Note: An encryption algorithm's block size will cause buffering, // and therefore not every loop iteration will produce output. li_Success = loo_Stream.WriteString(ls_PlainText) if loo_Stream.DataAvailable = 1 then ls_CipherText = loo_Stream.ReadBytesENC("hex") Write-Debug ls_CipherText li_Success = loo_EStrings.Append(ls_CipherText) end if next // Tell the background task that the stream has ended. li_Success = loo_Stream.WriteClose() // Let's make sure the background task finished. // It should already be the case that the task is finished. do while (loo_Task.Finished <> 1) loo_Task.SleepMs(20) loop // Get any remaining data available from the stream. if loo_Stream.DataAvailable = 1 then ls_CipherText = loo_Stream.ReadBytesENC("hex") Write-Debug ls_CipherText li_Success = loo_EStrings.Append(ls_CipherText) end if if loo_Task.TaskSuccess <> 1 then Write-Debug "async encryption failed:" Write-Debug loo_Task.ResultErrorText li_Success = 0 end if destroy loo_Task Write-Debug "-- encrypt finished --" // Now decrypt to return the original. // Reset the stream object so it can be used again. loo_Stream.Reset() // Start a background task that will decrypt a stream. loo_Task = loo_Crypt.DecryptStreamAsync(loo_Stream) li_Success = loo_Task.Run() n = loo_EStrings.Count for i = 0 to n - 1 li_Success = loo_Stream.WriteBytesENC(loo_EStrings.GetString(i),"hex") if loo_Stream.DataAvailable = 1 then ls_PlainText = loo_Stream.ReadString() Write-Debug ls_PlainText end if next // Tell the background task that the stream has ended. li_Success = loo_Stream.WriteClose() // Let's make sure the background task finished. // It should already be the case that the task is finished. do while (loo_Task.Finished <> 1) loo_Task.SleepMs(20) loop // Get any remaining data available from the stream. if loo_Stream.DataAvailable = 1 then ls_PlainText = loo_Stream.ReadString() Write-Debug ls_PlainText end if if loo_Task.TaskSuccess <> 1 then Write-Debug "async decryption failed:" Write-Debug loo_Task.ResultErrorText li_Success = 0 end if destroy loo_Task Write-Debug "-- decrypt finished --" destroy loo_Crypt destroy loo_Stream destroy loo_EStrings |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.