![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Decrypt File in Chunks using 256-bit AESSee more Encryption ExamplesShows how to decrypt a file chunk-by-chunk using FirstChunk/LastChunk properties and accumulate the results in memory with a Chilkat BinData object.
IncludeFile "CkBinData.pb" IncludeFile "CkFileAccess.pb" IncludeFile "CkCrypt2.pb" Procedure ChilkatExample() success.i = 0 ; 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 crypt.i = CkCrypt2::ckCreate() If crypt.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkCrypt2::setCkCryptAlgorithm(crypt, "aes") CkCrypt2::setCkCipherMode(crypt, "cbc") CkCrypt2::setCkKeyLength(crypt, 256) CkCrypt2::ckSetEncodedKey(crypt,"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F","hex") CkCrypt2::ckSetEncodedIV(crypt,"000102030405060708090A0B0C0D0E0F","hex") fileToDecrypt.s = "c:/temp/qa_output/hamlet.enc" facIn.i = CkFileAccess::ckCreate() If facIn.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkFileAccess::ckOpenForRead(facIn,fileToDecrypt) If success <> 1 Debug "Failed to open file to be decrytped." CkCrypt2::ckDispose(crypt) CkFileAccess::ckDispose(facIn) ProcedureReturn EndIf ; Let's decrypt in 32000 byte chunks. chunkSize.i = 32000 numChunks.i = CkFileAccess::ckGetNumBlocks(facIn,chunkSize) CkCrypt2::setCkFirstChunk(crypt, 1) CkCrypt2::setCkLastChunk(crypt, 0) bd.i = CkBinData::ckCreate() If bd.i = 0 Debug "Failed to create object." ProcedureReturn EndIf bdDecrypted.i = CkBinData::ckCreate() If bdDecrypted.i = 0 Debug "Failed to create object." ProcedureReturn EndIf i.i = 0 While i < numChunks i = i + 1 If i = numChunks CkCrypt2::setCkLastChunk(crypt, 1) EndIf ; Read the next chunk from the file. ; The last chunk will be whatever amount remains in the file.. CkBinData::ckClear(bd) CkFileAccess::ckFileReadBd(facIn,chunkSize,bd) ; Decrypt this chunk. CkCrypt2::ckDecryptBd(crypt,bd) ; Accumulate the decrypted chunks. CkBinData::ckAppendBd(bdDecrypted,bd) CkCrypt2::setCkFirstChunk(crypt, 0) Wend ; Make sure both FirstChunk and LastChunk are restored to 1 after ; encrypting or decrypting in chunks. Otherwise subsequent encryptions/decryptions ; will produce unexpected results. CkCrypt2::setCkFirstChunk(crypt, 1) CkCrypt2::setCkLastChunk(crypt, 1) CkFileAccess::ckFileClose(facIn) ; 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. CkBinData::ckWriteFile(bdDecrypted,"c:/temp/qa_output/hamlet_decrypted.xml") CkCrypt2::ckDispose(crypt) CkFileAccess::ckDispose(facIn) CkBinData::ckDispose(bd) CkBinData::ckDispose(bdDecrypted) ProcedureReturn EndProcedure |
||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.