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) Encrypt File in Chunks using AES CBCDemonstrates how to use the FirstChunk/LastChunk properties to encrypt a file chunk-by-chunk.
integer li_rc oleobject loo_Crypt string ls_FileToEncrypt oleobject loo_FacIn integer li_Success string ls_OutputEncryptedFile oleobject loo_FacOutEnc integer li_ChunkSize integer li_NumChunks oleobject loo_Bd integer i string ls_DecryptedFile integer li_BSame // 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 loo_Crypt.CryptAlgorithm = "aes" loo_Crypt.CipherMode = "cbc" loo_Crypt.KeyLength = 128 loo_Crypt.SetEncodedKey("000102030405060708090A0B0C0D0E0F","hex") loo_Crypt.SetEncodedIV("000102030405060708090A0B0C0D0E0F","hex") ls_FileToEncrypt = "qa_data/hamlet.xml" loo_FacIn = create oleobject // Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0 li_rc = loo_FacIn.ConnectToNewObject("Chilkat.FileAccess") li_Success = loo_FacIn.OpenForRead(ls_FileToEncrypt) if li_Success <> 1 then Write-Debug "Failed to open file that is to be encrytped." destroy loo_Crypt destroy loo_FacIn return end if ls_OutputEncryptedFile = "qa_output/hamlet.enc" loo_FacOutEnc = create oleobject // Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0 li_rc = loo_FacOutEnc.ConnectToNewObject("Chilkat.FileAccess") li_Success = loo_FacOutEnc.OpenForWrite(ls_OutputEncryptedFile) if li_Success <> 1 then Write-Debug "Failed to encrypted output file." destroy loo_Crypt destroy loo_FacIn destroy loo_FacOutEnc return end if // Let's encrypt in 10000 byte chunks. li_ChunkSize = 10000 li_NumChunks = loo_FacIn.GetNumBlocks(li_ChunkSize) loo_Crypt.FirstChunk = 1 loo_Crypt.LastChunk = 0 loo_Bd = create oleobject // Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData") i = 0 do while i < li_NumChunks i = i + 1 if i = li_NumChunks then loo_Crypt.LastChunk = 1 end if // Read the next chunk from the file. // The last chunk will be whatever amount remains in the file.. loo_Bd.Clear() loo_FacIn.FileReadBd(li_ChunkSize,loo_Bd) // Encrypt. loo_Crypt.EncryptBd(loo_Bd) // Write the encrypted chunk to the output file. loo_FacOutEnc.FileWriteBd(loo_Bd,0,0) loo_Crypt.FirstChunk = 0 loop // Make sure both FirstChunk and LastChunk are restored to 1 after // encrypting or decrypting in chunks. Otherwise subsequent encryptions/decryptions // will produce unexpected results. loo_Crypt.FirstChunk = 1 loo_Crypt.LastChunk = 1 loo_FacIn.FileClose() loo_FacOutEnc.FileClose() // Decrypt the encrypted output file in a single call using CBC mode: ls_DecryptedFile = "qa_output/hamlet_dec.xml" li_Success = loo_Crypt.CkDecryptFile(ls_OutputEncryptedFile,ls_DecryptedFile) // Assume success for the example.. // Compare the contents of the decrypted file with the original file: li_BSame = loo_FacIn.FileContentsEqual(ls_FileToEncrypt,ls_DecryptedFile) Write-Debug "bSame = " + string(li_BSame) destroy loo_Crypt destroy loo_FacIn destroy loo_FacOutEnc destroy loo_Bd |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.