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

发布于 2024-07-27 21:41:13 字数 1188 浏览 3 评论 0原文

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

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

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

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

这阻碍了:

必须首先以密钥实例调用未绑定方法 importKey() 参数(改为 str 实例)

API 文档说:

importKey(self, keystring, **kwds)

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

key(key, passphrase='PASSPHRASE')

但这会阻碍:

ezPyCrypto.CryptoKeyError:已尝试 导入无效密钥或密码 不好

文档链接:

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

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

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.

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:

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

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

This balks with:

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

The API doc says:

importKey(self, keystring, **kwds)

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

key(key, passphrase='PASSPHRASE')

but this balks with:

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假情假意假温柔 2024-08-03 21:41:13

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

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

但是,文档似乎表明这是执行您想要的操作的更好方法:

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

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

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')
清醇 2024-08-03 21:41:13

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

以下是 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

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.

Here is a sample RSA encryption/decription code:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文