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.

In reply to Aes or blowfish file encryption/decryption ? by crashev

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.