DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoCrypt
String sFileToDecrypt
Handle hoFacIn
Integer iChunkSize
Integer iNumChunks
Variant vBd
Handle hoBd
Handle hoBdDecrypted
Integer i
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
If (Not(IsComObjectCreated(hoCrypt))) Begin
Send CreateComObject of hoCrypt
End
Set ComCryptAlgorithm Of hoCrypt To "aes"
Set ComCipherMode Of hoCrypt To "cbc"
Set ComKeyLength Of hoCrypt To 256
Send ComSetEncodedKey To hoCrypt "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F" "hex"
Send ComSetEncodedIV To hoCrypt "000102030405060708090A0B0C0D0E0F" "hex"
Move "c:/temp/qa_output/hamlet.enc" To sFileToDecrypt
Get Create (RefClass(cComCkFileAccess)) To hoFacIn
If (Not(IsComObjectCreated(hoFacIn))) Begin
Send CreateComObject of hoFacIn
End
Get ComOpenForRead Of hoFacIn sFileToDecrypt To iSuccess
If (iSuccess <> True) Begin
Showln "Failed to open file to be decrytped."
Procedure_Return
End
// Let's decrypt in 32000 byte chunks.
Move 32000 To iChunkSize
Get ComGetNumBlocks Of hoFacIn iChunkSize To iNumChunks
Set ComFirstChunk Of hoCrypt To True
Set ComLastChunk Of hoCrypt To False
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
Get Create (RefClass(cComChilkatBinData)) To hoBdDecrypted
If (Not(IsComObjectCreated(hoBdDecrypted))) Begin
Send CreateComObject of hoBdDecrypted
End
Move 0 To i
While (i < iNumChunks)
Move (i + 1) To i
If (i = iNumChunks) Begin
Set ComLastChunk Of hoCrypt To True
End
// Read the next chunk from the file.
// The last chunk will be whatever amount remains in the file..
Get ComClear Of hoBd To iSuccess
Get pvComObject of hoBd to vBd
Get ComFileReadBd Of hoFacIn iChunkSize vBd To iSuccess
// Decrypt this chunk.
Get pvComObject of hoBd to vBd
Get ComDecryptBd Of hoCrypt vBd To iSuccess
// Accumulate the decrypted chunks.
Get pvComObject of hoBd to vBd
Get ComAppendBd Of hoBdDecrypted vBd To iSuccess
Set ComFirstChunk Of hoCrypt To False
Loop
// Make sure both FirstChunk and LastChunk are restored to True after
// encrypting or decrypting in chunks. Otherwise subsequent encryptions/decryptions
// will produce unexpected results.
Set ComFirstChunk Of hoCrypt To True
Set ComLastChunk Of hoCrypt To True
Send ComFileClose To hoFacIn
// 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.
Get ComWriteFile Of hoBdDecrypted "c:/temp/qa_output/hamlet_decrypted.xml" To iSuccess
End_Procedure