Question

I want to convert some existing AES code from M2Crypto to the equivalent pycrypto but the documentation is thin, especially for M2Crypto. I have reduced the relevant code to a gist. The main issues are:

  • Pycrypto requires the input to be multiple of 16 in length, m2crypto does not.
  • Even when the input length is multiple of 16, the ciphertext differs. After experimenting with M2Crypto cipher parameters, it turns out that setting padding and key_as_bytes to false encrypts to the same ciphertext with pycrypto. So I need to emulate padding=True and key_as_bytes=True in pure python.

Any help would be much appreciated.

EDIT: Solved - the gist has been updated with the equivalent M2Crypto/pycrypto code and tests, I'll leave it there in case someone finds it useful in the future.

Was it helpful?

Solution

You will have to implement PKCS#7 padding/unpadding, which is kind of simple and specified in the publicly available standard from RSA labs, and of course on Wikipedia. Also see this answer:

AES 256 Encryption with PyCrypto using CBC mode - any weaknesses?

Note that PKCS#7 padding and PKCS#5 padding are identical, although the latter is officially only for 8 byte block ciphers (e.g. DES/TDEA). OpenSSL uses PKCS#7 padding by default.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top