AES encryption / decryption between Ruby-OpenSSL, PyCrypto

I need to encrypt a piece of text using Ruby. For this purpose, I used the Ruby-Openssl gem. This ciphertext is passed to the python program, with which I must decrypt it. I used Pycrypto for this purpose.

The problem is that in Pycrypto we have to manually specify the add-on agreement. In Ruby, indentation is automatic. I use AES-CBC block encryption mode. This add-on causes problems because its removal cannot be performed correctly in Python. As an example, these are base64 encodings of ciphertext in Ruby and Python:

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

Please, help...

+5
source share
2 answers

OpenSSL uses PKCS # 5Padding by default, so it is also used automatically when encrypting data using OpenSSL::CipherAES-CBC mode (see OpenSSL docs ). Therefore, when using Ruby, there is no need to perform manual filling.

Filling must be done manually in Python when using PyCrypto.

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

+3
source

It looks like you need to specify the correct padding mode for use in both cases - padding is a fundamental property of the encryption stream and must match both the receiver and sender.

0
source

All Articles