[pycrypto] Typo or bug?

Dave Pawson dave.pawson at gmail.com
Tue Jun 26 02:52:32 EDT 2012

Just re-installing pycrypto for my own use. Version 2.6, with python 2.7
(src src http://www.turnkeylinux.org/blog/python-symmetric-encryption )
def _lazysecret(secret, blocksize=16, padding='}'):
   """pads secret if not legal AES block size (16, 24, 32)"""
   if not len(secret) in (16, 24, 32):
       return secret + (blocksize - len(secret)) * padding
   return secret

def decrypt(ciphertext, secret, lazy=True, checksum=True):
   """decrypt ciphertext with secret
   ciphertext  - encrypted content to decrypt
   secret      - secret to decrypt ciphertext
   lazy        - pad secret if less than legal blocksize (default: True)
   checksum    - verify crc32 byte encoded checksum (default: True)
   returns plaintext
   secret = _lazysecret(secret) if lazy else secret
   print secret, len(secret)
   encobj = AES.new(secret, AES.MODE_CFB)
   plaintext = encobj.decrypt(ciphertext)
   if checksum:
       crc, plaintext = (plaintext[-4:], plaintext[:-4])
       if not crc == struct.pack("i", zlib.crc32(plaintext)):
           print "Checksum error"
           raise CheckSumError("checksum mismatch")
   return plaintext

run this and I get

dkdkkd}}}}}}}}}} 16
Traceback (most recent call last):
 File "/files/python/fp/fp.py", line 148, in <module>
 File "/files/python/fp/fp.py", line 131, in domain
 File "/files/python/fp/fp.py", line 97, in decrypt
   encobj = AES.new(secret, AES.MODE_CFB)
 File "/usr/lib64/python2.7/site-packages/Crypto/Cipher/AES.py", line
95, in new
   return AESCipher(key, *args, **kwargs)
 File "/usr/lib64/python2.7/site-packages/Crypto/Cipher/AES.py", line
59, in __init__
   blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
 File "/usr/lib64/python2.7/site-packages/Crypto/Cipher/blockalgo.py",
line 141, in __init__
   self._cipher = factory.new(key, *args, **kwargs)
ValueError: IV must be 16 bytes long

So although the 'secret' is 16 bytes long, I'm getting a report saying it isn't?

Any suggestions please.


Dave Pawson
Docbook FAQ.

More information about the pycrypto mailing list