简体   繁体   English

Ruby-OpenSSL、PyCrypto 之间的 AES 加密/解密

[英]AES encryption/decryption between Ruby-OpenSSL, PyCrypto

I have to encrypt a piece of text using Ruby.我必须使用 Ruby 加密一段文本。 I've used the Ruby-Openssl gem for that purpose.为此,我使用了 Ruby-Openssl gem。 This encrypted text is them passed to a python program using which I have to decrypt it.这个加密的文本是他们传递给 python 程序的,我必须使用它来解密它。 I've used Pycrypto for the purpose.我为此目的使用了 Pycrypto。

The problem is, in Pycrypto we have to specify the padding convention manually.问题是,在 Pycrypto 中,我们必须手动指定填充约定。 In Ruby, the padding is done automatically.在 Ruby 中,填充是自动完成的。 I'm using AES-CBC block cipher mode.我正在使用 AES-CBC 分组密码模式。 This padding causes problems as its stripping cannot be performed properly in Python.这种填充会导致问题,因为它的剥离在 Python 中无法正确执行。 As an example, these are the base64 encodings of an encrypted text in both Ruby and Python:例如,这些是 Ruby 和 Python 中加密文本的 base64 编码:

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

Please help...请帮忙...

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 ). OpenSSL 默认应用 PKCS#5Padding,因此在使用OpenSSL::Cipher在 AES-CBC 模式下加密数据时也会自动使用(参见 OpenSSL文档)。 So there's no need to perform manual padding when using Ruby.所以在使用 Ruby 时无需手动填充。

The padding has to be done manually in Python when using PyCrypto.使用 PyCrypto 时,必须在 Python 中 手动完成填充。

Once you apply this padding scheme in Python, both encrypted Base64 strings should match.在 Python 中应用此填充方案后,两个加密的 Base64 字符串应该匹配。

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.看来您需要指定在这两种情况下使用的正确填充模式 - 填充是密码 stream 的基本属性,并且必须在接收方和发送方上匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM