use strict; use warnings; use Crypt::CBC qw( ); my $qfn = 'data.enc'; sub read_bytes { my ($fh, $to_read) = @_; my $buf = ''; while ($to_read) { my $bytes_read = read($fh, $buf, $to_read, length($buf)); die("$!\n") if !defined($bytes_read); die("Unexpected end of file\n") if !$bytes_read; $to_read -= $bytes_read; } return $buf; } sub read_uint32 { my ($fh) = @_; return (unpack('N', read_bytes($fh, 4))); } sub read_str { my ($fh) = @_; my $length = read_uint32($fh); return read_bytes($fh, $length); } sub write_uint32 { my ($fh, $n) = @_; print $fh (pack('N', $n)); } sub write_str { my ($fh, $str) = @_; print $fh (pack('N', length($str)), $str); } { my $key = Crypt::CBC->random_bytes(8); my $cipher = Crypt::CBC->new({ key => $key, cipher => 'Blowfish' }); { open(my $fh, '>', $qfn) or die; binmode($fh); write_str($fh, $cipher->encrypt("Hello World")); write_str($fh, $cipher->encrypt("foo bar")); } { open(my $fh, '<', $qfn) or die; binmode($fh); while (!eof($fh)) { print($cipher->decrypt(read_str($fh)), "\n"); } } }