使用OpenSSL生成密钥的Python RSA解密

时间:2022-01-23 18:30:29

Does anyone know the simplest way to import an OpenSSL RSA private/public key (using a passphrase) with a Python library and use it to decrypt a message.

有谁知道用Python库导入OpenSSL RSA私钥/公钥(使用密码)并使用它来解密消息的最简单方法。

I've taken a look at ezPyCrypto, but can't seem to get it to recognise an OpenSSL RSA key, I've tried importing a key with importKey as follows:

我看了一下ezPyCrypto,但似乎无法识别OpenSSL RSA密钥,我尝试使用importKey导入密钥,如下所示:

key.importKey(myKey, passphrase='PASSPHRASE')

myKey in my case is an OpenSSL RSA public/private keypair represented as a string.

在我的例子中,myKey是一个以字符串形式表示的OpenSSL RSA公钥/私钥对。

This balks with:

这个有:

unbound method importKey() must be called with key instance as first argument (got str instance instead)

必须使用键实例作为第一个参数调用unbound方法importKey()(改为使用str实例)

The API doc says:

API文档说:

importKey(self, keystring, **kwds)

importKey(self,keystring,** kwds)

Can somebody suggest how I read a key in using ezPyCrypto? I've also tried:

有人可以建议我如何阅读使用ezPyCrypto的密钥吗?我也尝试过:

key(key, passphrase='PASSPHRASE')

but this balks with:

但是这个:

ezPyCrypto.CryptoKeyError: Attempted to import invalid key, or passphrase is bad

ezPyCrypto.CryptoKeyError:尝试导入无效密钥,或密码错误

Link to docs here:

链接到这里的文档:

http://www.freenet.org.nz/ezPyCrypto/detail/index.html

EDIT: Just an update on this. Successfully imported an RSA key, but had real problem decrypting because eqPyCrypto doesn't support the AES block cipher. Just so that people know. I successfully managed to do what I wanted using ncrypt (http://tachyon.in/ncrypt/). I had some compilation issues with M2Crypto because of SWIG and OpenSSL compilation problems, despite having versions installed that exceeded the minimum requirements. It would seem that the Python encryption/decryption frameworks are a bit of a minefield at the moment. Ho hum, thanks for your help.

编辑:只是对此的更新。已成功导入RSA密钥,但由于eqPyCrypto不支持AES分组密码,因此解密时遇到实际问题。只是让人们知道。我成功地设法使用ncrypt(http://tachyon.in/ncrypt/)做我想做的事。尽管安装的版本超出了最低要求,但由于SWIG和OpenSSL编译问题,我在M2Crypto中遇到了一些编译问题。看起来Python加密/解密框架目前还是一个雷区。哼哼,谢谢你的帮助。

2 个解决方案

#1


The first error is telling you that importKey needs to be called on an instance of key.

第一个错误告诉您需要在key实例上调用importKey。

k = key()
k.importKey(myKey, passphrase='PASSPHRASE')

However, the documentation seems to suggest that this is a better way of doing what you want:

但是,文档似乎表明这是一种更好的方式来做你想做的事情:

k = key(keyobj=myKey, passphrase='PASSPHRASE')

#2


It is not clear what are you trying to achieve, but you could give M2Crypto a try. From my point of view it is the best OpenSSL wrapper available for Python.

目前尚不清楚你想要实现什么,但你可以试试M2Crypto。从我的角度来看,它是适用于Python的最佳OpenSSL包装器。

Here is a sample RSA encryption/decription code:

以下是RSA加密/解密代码示例:

import M2Crypto as m2c
import textwrap
key = m2c.RSA.load_key('key.pem', lambda prompt: 'mypassword')

# encrypt something:
data = 'testing 123'
encrypted = key.public_encrypt(data, m2c.RSA.pkcs1_padding)
print "Encrypted data:"
print "\n".join(textwrap.wrap(' '.join(['%02x' % ord(b) for b in encrypted ])))

# and now decrypt it:
decrypted = key.private_decrypt(encrypted, m2c.RSA.pkcs1_padding)
print "Decrypted data:"
print decrypted
print data == decrypted

#1


The first error is telling you that importKey needs to be called on an instance of key.

第一个错误告诉您需要在key实例上调用importKey。

k = key()
k.importKey(myKey, passphrase='PASSPHRASE')

However, the documentation seems to suggest that this is a better way of doing what you want:

但是,文档似乎表明这是一种更好的方式来做你想做的事情:

k = key(keyobj=myKey, passphrase='PASSPHRASE')

#2


It is not clear what are you trying to achieve, but you could give M2Crypto a try. From my point of view it is the best OpenSSL wrapper available for Python.

目前尚不清楚你想要实现什么,但你可以试试M2Crypto。从我的角度来看,它是适用于Python的最佳OpenSSL包装器。

Here is a sample RSA encryption/decription code:

以下是RSA加密/解密代码示例:

import M2Crypto as m2c
import textwrap
key = m2c.RSA.load_key('key.pem', lambda prompt: 'mypassword')

# encrypt something:
data = 'testing 123'
encrypted = key.public_encrypt(data, m2c.RSA.pkcs1_padding)
print "Encrypted data:"
print "\n".join(textwrap.wrap(' '.join(['%02x' % ord(b) for b in encrypted ])))

# and now decrypt it:
decrypted = key.private_decrypt(encrypted, m2c.RSA.pkcs1_padding)
print "Decrypted data:"
print decrypted
print data == decrypted