2017-06-08 28 views
3

私はPythonでRSA暗号化を試みています。公開鍵を使ってメッセージを暗号化し、暗号文をテキストファイルに書き込む公開鍵/秘密鍵が生成されました。NotImplementedError:代わりにCrypto.Cipher.PKCS1_OAEPモジュールを使用してください。

from Crypto.PublicKey import RSA 
from Crypto import Random 
import ast 

random_generator = Random.new().read 
key = RSA.generate(1024, random_generator) 

publickey = key.publickey() 

encrypted = publickey.encrypt('encrypt this message', 32) 

print('encrypted message:', encrypted) 
f = open('encryption.txt', 'w') 
f.write(str(encrypted)) 
f.close() 

f = open('encryption.txt', 'r') 
message = f.read() 

decrypted = key.decrypt(ast.literal_eval(str(encrypted))) 

print('decrypted', decrypted) 

f = open('encryption.txt', 'w') 
f.write(str(message)) 
f.write(str(decrypted)) 
f.close() 

しかし、私はアプリケーションを実行すると、今、私は次のエラーを取得:次のように私が使用していたコードがある私はCrypto.Cipher.PKCS1_OAEPを実装してみてくださいどのように

Traceback (most recent call last): 
    File "C:/Users/RedCode/PycharmProjects/AdvancedApps/Encryption/RSA Example.py", line 10, in <module> 
    encrypted = publickey.encrypt('encrypt this message', 32) 
    File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\Crypto\PublicKey\RSA.py", line 390, in encrypt 
    raise NotImplementedError("Use module Crypto.Cipher.PKCS1_OAEP instead") 
NotImplementedError: Use module Crypto.Cipher.PKCS1_OAEP instead 

どんなにを、エラーが解消されません。私は、インポートCrypto.Cipher.PKCS1_OAEPfrom Crypto.Cipher.PKCS1_OAEP import RSAfrom Crypto.Cipher.PKCS1_OAEP import Randomfrom Crypto.Cipher.PKCS1_OAEP import ast、およびimport Crypto.Cipherを試しましたが、助けられていませんでした。

私はfrom Crypto.Cipher.PKCS1_OAEP import RSAを試みたが、エラーがあった。

Traceback (most recent call last): 
    File "C:/Users/RedCode/PycharmProjects/AdvancedApps/Encryption/RSA Example.py", line 3, in <module> 
    from Crypto.Cipher.PKCS1_OAEP import RSA 
ImportError: cannot import name 'RSA' 

私は私のファイルをチェックし、私はRSAパッケージを持っています。

この問題を修正するにはどうすればよいですか?

+0

に同じ私は '輸入Crypto.Cipher'を欠けていると'試行のリストからCrypto.Cipher輸入PKCS1_OAEP'から、あなたにもそれらを試してみたのですか? –

+0

@MaartenBodewes私は今それを試しただけで、まだ動作しませんでした。私はこれらの試みで質問を更新しました。 – RedCode

答えて

5

newを使用してPKCS1_OAEPのインスタンスを作成し、それを使用してメッセージを暗号化/復号化する必要があります。

from Crypto.Cipher import PKCS1_OAEP 

encryptor = PKCS1_OAEP.new(publickey) 
encrypted = encryptor.encrypt(b'encrypt this message') 

と復号

decryptor = PKCS1_OAEP.new(key) 
decrypted = decryptor.decrypt(ast.literal_eval(str(encrypted))) 
+0

それは完璧に働いた、ありがとう! – RedCode

関連する問題