Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

September 6, 2007

Out of Memory Error when trying to encrypt a large file

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
	


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.