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) Encrypt File in Chunks using AES CBCDemonstrates how to use the FirstChunk/LastChunk properties to encrypt a file chunk-by-chunk.
; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. $oCrypt = ObjCreate("Chilkat.Crypt2") $oCrypt.CryptAlgorithm = "aes" $oCrypt.CipherMode = "cbc" $oCrypt.KeyLength = 128 $oCrypt.SetEncodedKey "000102030405060708090A0B0C0D0E0F","hex" $oCrypt.SetEncodedIV "000102030405060708090A0B0C0D0E0F","hex" Local $sFileToEncrypt = "qa_data/hamlet.xml" $oFacIn = ObjCreate("Chilkat.FileAccess") Local $bSuccess = $oFacIn.OpenForRead($sFileToEncrypt) If ($bSuccess <> True) Then ConsoleWrite("Failed to open file that is to be encrytped." & @CRLF) Exit EndIf Local $sOutputEncryptedFile = "qa_output/hamlet.enc" $oFacOutEnc = ObjCreate("Chilkat.FileAccess") $bSuccess = $oFacOutEnc.OpenForWrite($sOutputEncryptedFile) If ($bSuccess <> True) Then ConsoleWrite("Failed to encrypted output file." & @CRLF) Exit EndIf ; Let's encrypt in 10000 byte chunks. Local $iChunkSize = 10000 Local $iNumChunks = $oFacIn.GetNumBlocks($iChunkSize) $oCrypt.FirstChunk = True $oCrypt.LastChunk = False $oBd = ObjCreate("Chilkat.BinData") Local $i = 0 While $i < $iNumChunks $i = $i + 1 If ($i = $iNumChunks) Then $oCrypt.LastChunk = True EndIf ; Read the next chunk from the file. ; The last chunk will be whatever amount remains in the file.. $oBd.Clear() $oFacIn.FileReadBd($iChunkSize,$oBd) ; Encrypt. $oCrypt.EncryptBd($oBd) ; Write the encrypted chunk to the output file. $oFacOutEnc.FileWriteBd($oBd,0,0) $oCrypt.FirstChunk = False Wend ; Make sure both FirstChunk and LastChunk are restored to True after ; encrypting or decrypting in chunks. Otherwise subsequent encryptions/decryptions ; will produce unexpected results. $oCrypt.FirstChunk = True $oCrypt.LastChunk = True $oFacIn.FileClose $oFacOutEnc.FileClose ; Decrypt the encrypted output file in a single call using CBC mode: Local $sDecryptedFile = "qa_output/hamlet_dec.xml" $bSuccess = $oCrypt.CkDecryptFile($sOutputEncryptedFile,$sDecryptedFile) ; Assume success for the example.. ; Compare the contents of the decrypted file with the original file: Local $bSame = $oFacIn.FileContentsEqual($sFileToEncrypt,$sDecryptedFile) ConsoleWrite("bSame = " & $bSame & @CRLF) |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.