PyCryptodome RSA加密

问题描述 投票:0回答:1

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

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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.