brettviren/python-keepass

Input strings must be a multiple of 16 in length

Opened this issue · 2 comments

Hi. Does this code work on Windows?
I'm attempting to run it on:

  • Windows 7
  • Python 2.7
  • PyCrypto 2.6 for Python 2.7
  • A Keepass 1.24 database file

I get the following stack trace:

Traceback (most recent call last):
File "keepassc.py", line 10, in
cliobj()
File "./python\keepass\cli.py", line 78, in call
meth(cmdopts)
File "./python\keepass\cli.py", line 143, in _open
self.db = kpdb.Database(files[0],opts.masterkey)
File "./python\keepass\kpdb.py", line 29, in init
self.read(filename)
File "./python\keepass\kpdb.py", line 55, in read
self.header.encryption_iv)
File "./python\keepass\kpdb.py", line 99, in decrypt_payload
payload = self.decrypt_payload_aes_cbc(payload, finalkey, iv)
File "./python\keepass\kpdb.py", line 116, in decrypt_payload_aes_cbc
payload = cipher.decrypt(payload)
File "C:\Python27\lib\site-packages\Crypto\Cipher\blockalgo.py", line 295, in decrypt
return self._cipher.decrypt(ciphertext)
ValueError: Input strings must be a multiple of 16 in length

I've not tested this on Windows. It was developed on Debian. The error you show does not look to me to be related to platform differences. It's been a while that I've used this code so maybe this multiple of 16 is a new requirement in the Crypto module(?).

Is it possible to zero-pad the ciphertext payload out to a multiple of 16 before the decrypt and recover the plaintext?

Better late than never: I found the problem on Windows being that read() stops at EOF when the file is not explicitly opened in binary mode. See also http://stackoverflow.com/questions/9905874/python-does-not-read-entire-text-file
The payload of a database file must always be a multple of 16 bytes.

Opening the database with fp = open(filename, "rb") (kpdb.py:Database:read) solves the problem.