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

Encrypting large files

Thread Next
From:
Fabian Gut
Date:
April 7, 2010 00:55
Subject:
Encrypting large files
Message ID:
4BBC3ACA.5040701@open.ch
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

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About