Adrian
Adrian

Reputation: 31

Encrypt file in Perl

I created the algorithm to encrypt and decrypt a string in Perl ( using AES in CFB mode). Now I want to extend to encryption to file level. How should I get the content of the file? What would be a good approach?

  1. Read the file normally
    open(my $fh, "<", "myTestFile.ext");
  2. Read the file in binmode
    open(my $fh, "<", "myTestFile.ext"); binmode $fh

Then how should I store the content of the files?

a) Read all the content of the file in one string and provide the string to the implemented program

my $document = do {
    local $/ = undef;
    <$fh>; # file handle opened previously
};
encryptionAlgorithm($document);

b) Read the content of the file line by line

while( my $line = <$fh>)
{
    encryptionAlgorithm($line);
}

In both cases should I chomp the \n's ?

Upvotes: 1

Views: 1818

Answers (1)

Dada
Dada

Reputation: 6626

AES encrypts blocks of 128 bits (16 bytes), so you'll want to read your file 16 bytes at a time. To do this, you need to binmode your file, then read it with the read builtin:

open my $fh, '<', 'myTestFile.ext' or die $!;
binmode $fh;

while (read($fh,my $block,16)) {
    # encrypt $block
}

Note that I've added or die $! after opening the file: you want to always make sure your open worked.

Also, don't forget that if the block you read is less than 16 bytes long, you'll have to do some padding. (I don't recall how the blocks are padded for AES, but I trust you do since you are implementing it)


About the approaches you thought of:

  • Reading the entire file at once will potentially consume a lot of memory if the file is big.

  • Reading the file line by line: if the file contains no newline, then you'll read it entirely at once, which might consume a lot of memory. And if lines contain a number of bytes which isn't a multiple of 16, then you'll have to combine bytes from different lines, which will require more work than simply reading blocks of 16 bytes.

Also, you definitely don't want to chomp anything! You should have decrypt(encrypt(file)) == file, but if you chomp the newlines, that won't be the case anymore.

Upvotes: 2

Related Questions