Question
While trying to encrypt a zip file it raise the error "Out of memory".
We are using the algorithm type "AES".
We need to encrypt big zip files that could be a complete backup file of a user’s computer.
This is our code:
set crypt = CreateObject("ChilkatCrypt2.ChilkatCrypt2")
crypt.UnlockComponent "MyUnlockCode"
crypt.CryptAlgorithm = "AES"
crypt.KeyLength = 128
crypt.SecretKey = crypt.GenerateSecretKey("secret")
myData = crypt.ReadFile("big.zip")
myEncrypted = crypt.EncryptBytes(myData)
crypt.WriteFile "big.enc",myEncrypted
Solution
It’s not a good idea to try to read a file that large into memory — your computer certainly does not have enough memory to handle it.
The solution is to use the file-to-file streaming encryption feature provided by Chilkat Crypt2. The file-to-file encryption/decryption methods are CkEncryptFile(inFilename,outFilename) and CkDecryptFile(inFilename,outFilename). These methods will encrypt files in streaming mode, using any of the supported encryption algorithms.
This is the code rewritten to encrypt large files:
set crypt = CreateObject("ChilkatCrypt2.ChilkatCrypt2")
crypt.UnlockComponent "MyUnlockCode"
crypt.CryptAlgorithm = "AES"
crypt.KeyLength = 128
crypt.SecretKey = crypt.GenerateSecretKey("secret")
success = crypt.CkEncryptFile("big.zip","big.enc")
if (success = 0) then
MsgBox crypt.LastErrorText
end if