http://qs321.pair.com?node_id=155565


in reply to Basic Crypt::Blowfish usage question

Your problem is two-fold: First, as others have already pointed out: Blowfish is an 8-byte block encryption scheme, so you either need to pass it a string to encrypt of just 8 bytes, pad out a shorter string with nulls (\0), or work with longer data in 8-byte blocks (with padding when necessary) or get Crypt::CBC or Crypt::CBCeasy to deal with arbitrary length strings. Secondly (but your more immediate problem), your first encryption run prints out an unpacked version of the encrypted string (which is fine, makes it easier to read and type back in for the decrypt run right?) ... but in your decrypt run you try to decrypt that string as entered, you'll have to pack it up again before decrypting. Try this slight modification to your enc/dec pair:

# --- enc.pl --- #!/usr/bin/perl -w use strict; use Crypt::Blowfish; my $plaintext = <>; chomp $plaintext; my $key = "this is the pass phrase"; my $cipher = Crypt::Blowfish->new($key); my $ciphertext = $cipher->encrypt($plaintext); print unpack ("H16", $ciphertext),"\n"; # --- dec.pl --- #!/usr/bin/perl -w use strict; use Crypt::Blowfish; my $ciphertext = <>; chomp $ciphertext; my $key = "this is the pass phrase"; my $cipher = Crypt::Blowfish->new($key); my $plaintext = $cipher->decrypt(pack "H16",$ciphertext); print $plaintext,"\n"; # --- sample session (using 8-byte plaintext only) --- $ perl enc.pl noseeums <== what I type 8db8af77b828c382 <== what I get $ perl dec.pl 8db8af77b828c382 <== what I type noseeums <== what I get $ echo noseeums|perl enc.pl |perl dec.pl noseeums