[PYTHON-CRYPTO] amkCrypto: why 2-byte conversion for str2hex()?

Jason R. Mastaler jason-list-python-crypto at MASTALER.COM
Tue Mar 27 23:44:28 CEST 2001


Why does Crypto.Utils.str2hex() convert each byte of its string to a
2-byte HEX representation (as opposed to a 1-byte HEX representation).

I ask because this might account for the difference in output I'm
seeing between two functionally equivalent pieces of code (one in
Perl, the other in Python).

        #!/usr/bin/env python

        import time
        from Crypto.Cipher import Blowfish
        key = '146bfea4ab7274e6f0ccff25351c2f39'
        cipherobj=Blowfish.new(key, Blowfish.CBC)
        input =  '%16d' % (time.time() +864000)
        from Crypto.Utils import str2hex
        dated_cookie = str2hex(cipherobj.encrypt(input))
        print dated_cookie

        -------------------------------------------------

        #!/usr/bin/perl

        use Crypt::Blowfish;
        $key = "146bfea4ab7274e6f0ccff25351c2f39";
        $cipherobj = new Crypt::Blowfish $key;
        $input = sprintf("%16d",time+864000);
        $dated_cookie = unpack("H*",$cipherobj->encrypt(pack("H*", $input)));
        print "$dated_cookie\n";

Their output:

        $ python datedcookie.py
        ec6bc328cbf0ab8c24a3ab68258bf773

        $ perl datedcookie.pl
        c9c7b6f9cb96e31c

As you can see, the Python code results in a string which is twice as
long as the original one, while the Perl code's string is the same
length as the original.  So it seems Perl converts to 1-byte HEX
instead of two.  Is there a way to convert to one-byte HEX in Python
as well?





More information about the python-crypto mailing list