LCJ
LCJ

Reputation: 22652

Module 'Crypto.PublicKey.RSA' has no attribute 'import_key'

I have a C# application that uses following public keys to encrypt and decrypt (using corresponding private key) successfully. I am using the same Public Key in Python to encrypt, using following code. I am getting following error:

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

I referred RSA - pycryptodome.readthedocs.io to check 'import_key'. It says extern_key (string or byte string).

Note: I have pycrypto 2.6.1 installed. Verified using conda list.

How to fix this so that Python can use the same key that was successfully used by C# application?

Python Code

import Crypto
from Crypto.PublicKey import RSA
pubkey = 'BgIAAACkAABSU0ExAAQAAAEAAQB5ad3IFUIQ+NJeJEVlHJb0BaXhPCpeP+477ql+2dsNLzpn+3f2Lm5UWZhig60rx7/5/rAlAH+emU6WwOZNxtMtFbdu9CNBUjRp9FiEmJHZLaGqqmEFDyp287k3HVCFVzxxRAEy8ftL/q6KaE3KKrHoFiMozretUXulYy5OE1yR7w=='

import base64
decodedPublicKey = base64.b64decode(pubkey)
pub_key = RSA.import_key(decodedPublicKey)
encrypted = pub_key.encrypt('hello world', None)
print(encrypted)
text = pvt_key.decrypt(encrypted)
print(text)

Upvotes: 0

Views: 3984

Answers (1)

selmer80
selmer80

Reputation: 11

Did you install it recently? We just noticed an issue with the Raspberry Pi devices we built yesterday. In /usr/local/lib/python3.5/dist-packages/Crypto/PublicKey folder, many of the files(including RSA.py) were empty. We just uninstalled and reinstalled pycryptodome a few minutes ago and the files are no longer empty.

Upvotes: 1

Related Questions