Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AES encryption/decryption between Ruby-OpenSSL, PyCrypto

I have to encrypt a piece of text using Ruby. I've used the Ruby-Openssl gem for that purpose. This encrypted text is them passed to a python program using which I have to decrypt it. I've used Pycrypto for the purpose.

The problem is, in Pycrypto we have to specify the padding convention manually. In Ruby, the padding is done automatically. I'm using AES-CBC block cipher mode. This padding causes problems as its stripping cannot be performed properly in Python. As an example, these are the base64 encodings of an encrypted text in both Ruby and Python:

Python: aENJY28lvE89yY2T/te8vWwdeoeSqSwwlrOAv7b3AWw=
Ruby:   aENJY28lvE89yY2T/te8vVoQE6JNxdSRgYXC8mqF3nI=

Please help...

like image 389
vishy1618 Avatar asked Oct 25 '22 03:10

vishy1618


1 Answers

OpenSSL applies PKCS#5Padding by default, so this is also used automatically when encrypting data with OpenSSL::Cipher in AES-CBC mode (cf. OpenSSL docs). So there's no need to perform manual padding when using Ruby.

The padding has to be done manually in Python when using PyCrypto.

Once you apply this padding scheme in Python, both encrypted Base64 strings should match.

like image 79
emboss Avatar answered Oct 27 '22 11:10

emboss