【问题标题】:PyCryptodome RSA encryptionPyCryptodome RSA 加密
【发布时间】:2017-11-10 08:46:36
【问题描述】:

我正在尝试使用 pycryptodome 示例使用 RSA 密钥加密文件。示例如下

from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP

file_out = open("encrypted_data.bin", "wb")

recipient_key = RSA.import_key(open("receiver.pem").read())
session_key = get_random_bytes(16)

# Encrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(recipient_key)
file_out.write(cipher_rsa.encrypt(session_key))

# Encrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
[ file_out.write(x) for x in (cipher.nonce, tag, ciphertext) ]

我收到的错误是

AttributeError:模块“Crypto.PublicKey.RSA”没有属性“import_key”

我发现了另一个线程,该错误被确定为 pyCrypto 的版本问题,但我正在尝试使用 PyCryptodome 并且我确实拥有最新版本。

【问题讨论】:

    标签: python cryptography rsa python-cryptography pycryptodome


    【解决方案1】:

    PyCryptodome v3.4 中添加了方法import_key。 如果您收到该错误消息,则表示您实际上使用的是 PyCrypto 或旧版本的 PyCryptodome。

    【讨论】:

    • 如何确保我使用的是正确的版本而不是 PyCrypto?
    • 您可以查看Cipher.version_info 元组。 PyCryptodome 的版本 >=3.0.0。或者你可以安装pycryptodomex,它仍然是PyCryptodome,但在Cryptodome包下(而不是Crypto,以避免冲突)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 2015-02-27
    • 2012-03-13
    • 2013-07-15
    • 2012-11-15
    • 1970-01-01
    相关资源
    最近更新 更多