Sample code for 30+ languages & platforms
AutoIt

Decrypt File in Chunks using 256-bit AES

See more Encryption Examples

Shows how to decrypt a file chunk-by-chunk using FirstChunk/LastChunk properties and accumulate the results in memory with a Chilkat BinData object.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

; This example decrypts the file previously encrypted in this example:
; Encrypt File in Chunks using AES CBC

$oCrypt = ObjCreate("Chilkat.Crypt2")

$oCrypt.CryptAlgorithm = "aes"
$oCrypt.CipherMode = "cbc"
$oCrypt.KeyLength = 256

$oCrypt.SetEncodedKey "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F","hex"
$oCrypt.SetEncodedIV "000102030405060708090A0B0C0D0E0F","hex"

Local $sFileToDecrypt = "c:/temp/qa_output/hamlet.enc"
$oFacIn = ObjCreate("Chilkat.FileAccess")
$bSuccess = $oFacIn.OpenForRead($sFileToDecrypt)
If ($bSuccess <> True) Then
    ConsoleWrite("Failed to open file to be decrytped." & @CRLF)
    Exit
EndIf

; Let's decrypt in 32000 byte chunks.
Local $iChunkSize = 32000
Local $iNumChunks = $oFacIn.GetNumBlocks($iChunkSize)

$oCrypt.FirstChunk = True
$oCrypt.LastChunk = False

$oBd = ObjCreate("Chilkat.BinData")
$oBdDecrypted = 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)

    ; Decrypt this chunk.
    $oCrypt.DecryptBd($oBd)
    ; Accumulate the decrypted chunks.
    $oBdDecrypted.AppendBd($oBd)

    $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 

; The fully decrypted file is contained in bdDecrypted.
; You can save to a file if desired, or use the decrypted data in your application directly from bdDecrypted.
$oBdDecrypted.WriteFile("c:/temp/qa_output/hamlet_decrypted.xml")