crashev has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm trying to do some kind of encryption/decryption in PERL, no matter what I use (aes/rijandael or blowfish) result is the same - decrypted file is different from the oryginal one, I think I've tryed everything and don't know where the problem lies, here are example codes I've been using: (I have to add that they are supposed to be used with binary files not plaintext files)
#!/usr/bin/perl

use Crypt::CBC;

$cipher = Crypt::CBC->new( -key    => 'my secret key',
                         -cipher => 'Blowfish',
                         -header => 'none',
                         -iv => 'dupajasi'
);



$cipher->start('encrypting');

$sourcefile="fs9419105v001s.zip";

open(OUTF, ">$sourcefile.perl.crypt") || die;
open(F,"fs9419105v001s.zip");
print "? Encrypting ... \n";
  while (read(F,$buffer,1024)) {
      print OUTF $cipher->crypt($buffer);
  }
  print OUTF $cipher->finish;

close(OUTF);



print "? Decrypting.,..... \n";
$cipher2 = Crypt::CBC->new( -key    => 'my secret key',
                         -cipher => 'Blowfish',
                         -header => 'none',
                         -iv => 'dupajasi'
);

open(OUTF, ">$sourcefile.perl.decrypt") || die;
open(F,"fs9419105v001s.zip.perl.crypt");
  while (read(F,$buffer,1024)) {
      print OUTF $cipher2->decrypt($buffer);
  }
  print OUTF $cipher2->finish;

close(OUTF);




OR 



use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use POSIX;
use File::Basename;
use Crypt::CBC;
use Digest::MD5 qw(md5 md5_hex);

        # a) dencrypt
        my $key="ed8677a0fc735e9bf1d7bca3310bb82854b5217589f2e00dd3bab399be9707a7";
        my $sourcefile="fs9419105v001s.zip";

        my $cipher = Crypt::CBC->new( {'key' => substr($key,0,32),
                                'cipher'=> 'Rijndael',
                                'blocksize' => 16,
                                'iv' => substr($key,32,16),
                                'regenerate_key' => 0,
                                'padding' => 'standard',
                                'prepend_iv' => 0,
                                'header' => 'none',
                                   });

        open (INF1, "< ".$sourcefile) || die;
        open (OUTF1, "> ".$sourcefile.".perl.encrypt") || die;

        $file_size = -s $sourcefile;
        print "Sourcefile: $sourcefile $file_size \n";
        print "block size: ".$cipher->blocksize()."\n"; 
        print "? Encrypting ... \n";
        while (my $size = read(INF1,my $buf, 8192 ) ) {     
                        $count += $size;
                        $buf2 = $cipher->encrypt($buf);
                        print OUTF1 $buf2;
        }
                        print OUTF1 $cipher->finish();              

        close(INF1);
        close(OUTF1);   

        # a) decrypt

        my $sourcefile="fs9419105v001s.zip.perl.encrypt";
        open (INF1, "< ".$sourcefile) || die;
        open (OUTF1, "> ".$sourcefile.".decrypt") || die;


        $file_size = -s $sourcefile;
        print "Sourcefile: $sourcefile $file_size \n";
        print "block size: ".$cipher->blocksize()."\n"; 
        print "? Decrypting ... \n";
    
        while (my $size = read(INF1,my $buf, 8192 ) ) {     
                        $count += $size;
                        $buf2 = $cipher->decrypt($buf);
                        print OUTF1 $buf2;
        }
                            print OUTF1 $cipher->finish();                  

        close(INF1);
        close(OUTF1);   

both failed to do thieir jobs - I suppose I'm missing something perl specific here. Decrypted file size and sums are always different than the oryginal one.
  • Comment on Aes or blowfish file encryption/decryption ?

Replies are listed 'Best First'.
Re: Aes or blowfish file encryption/decryption ?
by moritz (Cardinal) on Dec 02, 2010 at 15:03 UTC
    Try to use binmode on all opened file handles. Something like
    use autodie; # to get better and automatic error messages open my $IN, "<:raw", 'filename.zip'; binmode $IN; my $contents = do { local $/; <$IN> }; close $IN; # and then work with $contents

    See also: perlopentut, open, PerlIO.

    Update: After actually reading PerlIO it seems that :raw and binmode do the same thing, though it can't hurt to test them both together.

      :raw and binmode don't do the same thing. :raw will remove buffering while binmode will preserve it.

      open my $IN, "<:raw:perlio", 'filename.zip';
      or
      open my $IN, "<", 'filename.zip'; binmode $IN;
Re: Aes or blowfish file encryption/decryption ?
by locked_user sundialsvc4 (Abbot) on Dec 02, 2010 at 18:15 UTC

    There’s a lot to be said for mod:://Crypt::CBCeasy, whether you use the actual module or simply refer to it.

Re: Aes or blowfish file encryption/decryption ?
by ikegami (Patriarch) on Dec 02, 2010 at 20:30 UTC
    Let Crypt::CBC pick a random IV instead of specifying one. You are defying the purpose of using an IV (salting) when you always use the same IV.