develooper Front page | perl.crypto | Postings from April 2010

Encrypting large files

Thread Next
Fabian Gut
April 7, 2010 00:55
Encrypting large files
Message ID:

I have a script that reads an uploaded file, encrypts it and saves it. 
The code I use is this:

# Open and read the uploaded file
my $UPLOADFILE = $q->param('upfile');
flock( $UPLOADFILE, 1 );
local $/;
my $data = <$UPLOADFILE>;

# Encrypt the file
my $cipher = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish' );
my $enc_data = $cipher->encrypt($data);

# Save the file.
open( SAFEFILE, ">", "$file" )
     or die "FATAL ERROR: Could not create $file : $!";
flock( SAFEFILE, 2 );
print SAFEFILE $enc_data;

Where $q is a CGI object, $key holds a string ("password") and $file is 
the filename for the new file.

This works perfectly. However, it might become a memory issue if large 
files (several 100 MB) are uploaded. Is there a better (or easier) way 
to encrypt large files than writing the CBC myself so I can read/write 
the files block by block?

Fabian Gut

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About