in reply to Crypt::CBC with Blowfish problem

If you slurp the encrypted data in and decrypt the whole string in one go it does not corrupt:

open(INF1, "<", "$0.crypt") || die; open(OUTF1, ">$0.decrypt") || die; local $/=undef; my $in = <INF1>; print OUTF1 $cipher->decrypt($in); close(INF1); close(OUTF1);

There are ten types of people: those that understand binary and those that don't.

Replies are listed 'Best First'.
Re^2: Crypt::CBC with Blowfish problem
by zentara (Cardinal) on Jan 06, 2006 at 15:46 UTC
    Thanks for pointing that out. I also found a workable snippet at Something aint right using Crypt::CBC here....
    #!/usr/bin/perl use warnings; use strict; use Crypt::CBC; my $KEY = 'secret_foo'; encryptFile( $0 ); decryptFile( "$0.enc"); sub encryptFile { my $filename = shift; $| = 1; print "Encrypting $filename..."; $| = 0; my $cipher = Crypt::CBC->new( -key => $KEY, -cipher => 'Blowfish', -padding => 'space', -add_header => 1 ); $cipher->start( 'encrypting' ); open( ORGINAL, "<./$filename" ); open( ENCRYPTED, ">./$filename.enc" ); binmode ORGINAL; binmode ENCRYPTED; while ( sysread( ORGINAL, my $buffer, 1024 ) ) { syswrite( ENCRYPTED, $cipher->crypt( $buffer ) ); } #$cipher->finish; #wrong syswrite( ENCRYPTED, $cipher->finish ); close ENCRYPTED; close ORGINAL; print "done.\n"; } ################################################################### sub decryptFile { my $filename = shift; $| = 1; print "Decrypting $filename..."; $| = 0; my $cipher = Crypt::CBC->new( -key => $KEY, -cipher => 'Blowfish', -padding => 'space', -add_header => 1 ); $cipher->start( 'decrypting' ); open( ENCRYPTED, "<./$filename" ); open( DECRYPTED, ">./$filename.decrypted" ); binmode ENCRYPTED; binmode DECRYPTED; while ( sysread( ENCRYPTED, my $buffer, 1024 ) ) { syswrite( DECRYPTED, $cipher->crypt( $buffer ) ); } # $cipher->finish; #wrong syswrite( DECRYPTED, $cipher->finish ); close DECRYPTED; close ENCRYPTED; print "done.\n"; }

    I'm not really a human, but I play one on earth. flash japh