2017-11-10 59 views
0

私は、RSA鍵でファイルを暗号化するpycryptodomeの例を使用しようとしています。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) ]

を次のように例があると私は、受信エラーが、私はこのエラーがpyCryptoとバージョンの問題として確認された別のスレッドを見つけましたが、私はしようとしています

AttributeError: module 'Crypto.PublicKey.RSA' has no attribute 'import_key'

ですPyCryptodomeを使用するには最新のバージョンが必要です。

答えて

0

import_keyPyCryptodome v3.4に追加されました。 このエラーメッセージが表示された場合は、実際にはPyCryptoまたは古いバージョンのPyCryptodomeを使用していることを意味します。

+0

PyCryptoではなく、正しいバージョンを使用していることを確認するにはどうすればよいですか? – jaydoe

+0

'Cipher.version_info'タプルを確認できます。 PyCryptodomeのバージョンは3.0.0以上になります。あるいは、 'pycryptodomex'をインストールすることもできます。これはPyCryptodomeですが、' Cryptodome'パッケージ(衝突を避けるために 'Crypto'の代わりに)の下にあります。 – SquareRootOfTwentyThree