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

Hi
Heres the code:
use Blowfish; $blowfishkey=pack("H*",'5b3b272e5d39342d33313d3d2d252640215e2b5d00'); $packet = pack("H*",'071bdba4419a1e4e'); $cipher = new Crypt::Blowfish $blowfishkey; print "Key: $blowfishkey\n"; print "Crypted packet: $packet\n"; $packet = $cipher->decrypt($packet); print "Decrypted packet: $packet\n"; $packet = "\x00WildMaN"; print "Initial known packet: $packet\n"; $packet = $cipher->encrypt($packet); print "Should be encrypted as: $packet\n";
I know the blowfish key. I know the string that was encrypted. I know the final encrypted packet. But if i decrypt it with perl Blowfish, or encrypt initial packet - it does not work, result is trash.

I've tried it on Java - works fine. I've tried it on C# - perfect. But on Perl it outputs trash. The only thing i can suggest is some internal troubles with string encodings, or maybe a bug within Crypt::Blowfish (my initial packet starts with zero character)... I have no ideas left, it works perfect on Java and C#.

Instead of "\x00WildMaN" (0057696c644d614e) i get this trash as decryption: 090216d5bf90386d.

20040915 Edit by castaway: Changed title from 'Problem with Blowfish'

Replies are listed 'Best First'.
Re: Rountrip through Crypt::Blowfish corrupts data?
by hv (Prior) on Sep 14, 2004 at 12:31 UTC

    Where did you get that initial encrypted packet from? If I use the following code instead, it shows that Crypt::Blowfish can do the round trip fine:

    use Crypt::Blowfish; $blowfishkey=pack("H*",'5b3b272e5d39342d33313d3d2d252640215e2b5d00'); $p1 = "\x00WildMaN"; $cipher = new Crypt::Blowfish $blowfishkey; print "Key: $blowfishkey\n"; print "Plaintext packet: $p1\n"; $p2 = $cipher->encrypt($p1); print "Encrypted packet: $p2\n"; $p3 = $cipher->decrypt($p2); print "Decrypted back to: $p3\n";

    Results:

    Key: [;'.]94-31==-%&@!^+]. Plaintext packet: .WildMaN (00 57 69 6c 64 4d 61 4e) Encrypted packet: ..Y..D.. (02 b2 59 1f f3 44 86 aa) Decrypted back to: .WildMaN (00 57 69 6c 64 4d 61 4e)

    Maybe your problem lies in the way the key is used to initialise the S-boxes, done in Crypt::Blowfish by the C function blowfish_make_bfkey() in _blowfish.c. But I don't know how you'd track down the difference short of analysing the code for this implementation, and comparing to the code in the Java and C# implementations.

    Hugo

Re: Rountrip through Crypt::Blowfish corrupts data?
by zentara (Cardinal) on Sep 14, 2004 at 14:05 UTC
    You are better off using Crypt::CBC when using blowfish and other similar encryption schemes, since it will setup things properly for you. Read perldoc Crypt::CBC, the first example is for using blowfish. From my experience, you need to need to properly set the "padding" for the keys and data, which CBC handles for you. From the NOTES: from perldoc Crypt::Blowfish

    NOTES The module is capable of being used with Crypt::CBC. You're encouraged to read the perldoc for Crypt::CBC if you intend to use this module for Cipher Block Chaining modes. In fact, if you have any intentions of encrypting more than eight bytes of data with this, or any other block cipher, you're going to need some type of block chaining help. Crypt::CBC tends to be very good at this. If you're not going to encrypt more than eight bytes, your data must be exactly eight bytes long. If need be, do your own padding. "\0" as a null byte is perfectly valid to use for this. Additionally, the current maintainer for Crypt::Blowfish may or may not release Crypt::CBC_R which replaces the default 'RandomIV' initialization vector in Crypt::CBC with a random initialization vector. (to the limits of /dev/urandom and associates) In either case, ........

    I'm not really a human, but I play one on earth. flash japh
Re: Rountrip through Crypt::Blowfish corrupts data?
by tachyon (Chancellor) on Sep 15, 2004 at 07:39 UTC
      Dunno, neither of them have any options to set the mode. Just default, and it works perfect. Well, I've made my own blowfish module for Perl based on C# code, so that is a workaround. Thanx all for replies.