Encrypting large files
[email protected] (Fabian Gut) Wed, 07 Apr 2010 09:56:58 +0200
| Newsgroups | perl.crypto |
|---|---|
| Organization | Open Systems AG - http://www.open.ch |
| Message-ID | <[email protected]> |
Hello
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>;
close($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;
close(SAFEFILE);
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?
Greetings
Fabian Gut