5

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...

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried to use M2Crypto. Unfortunately it does not seem to work with the program provided in the link. I'll try to apply the PKCS#5 padding.
0

It appears you need to be specifying the correct padding mode to use in both cases - padding is a fundamental property of a cipher stream, and must be matched on both receiver and sender.

3 Comments

The padding can be manually manipulated in PyCrypto, i.e, you are specifying the logic by which you do the padding. In Ruby on the other hand, padding is done automatically, I don't see any way to configure it other than duplicate the logic in Python.
You either need to do in Python what is being done automatically in Ruby or do it manually in both.
I haven't figured out a way to do it manually in Ruby. Which leaves me with modifying my python program, except I don't know how padding is done in Ruby-Openssl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.