I want to encrypt some data in python with PyCrypto.
However I get an error when using key = RSA.importKey(pubkey):
RSA key format is not supported
The key was generated with:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mycert.key -out mycert.pem
The code is:
def encrypt(data):
pubkey = open('mycert.pem').read()
key = RSA.importKey(pubkey)
cipher = PKCS1_OAEP.new(key)
return cipher.encrypt(data)
PyCrypto does not support X.509 certificates. You must first extract the public key with the command:
Then, you can use
RSA.importKeyonpublickey.pem.If you don’t want or cannot use openssl, you can take the PEM X.509 certificate and do it in pure Python like this: