0

I have used the below example for encrypting and decrypting some text in python and it works:

RSA encryption and decryption in Python

However when I write the private key to a file for future use and then import it to decrpyt some text I get:

"ValueError: RSA key format is not supported."

I have exported the private key to a file using:

privkey = key.exportKey()

f= open("/home/sam/samomate.pem","w+")
f.write(privkey)
f.close()

Try to import it using:

pkey = f.read()
keyDER = b64decode(pkey)
privkey=RSA.importKey(keyDER, passphrase=None)

I added the base64 decode on the back of googleing the error to no avail.

Any assistance on this or better alternatives would be greatly appreciated.

Cheers

Sam
  • 161
  • 1
  • 14

1 Answers1

1

I tried running this code and to me it works.

I didn't use the b64decode, that is not expected.

I'm not sure you've omitted any lines of code but to read a file you should use:

pkey_file = open(filename, "r")
pkey = f.read()

Then this line did not raise a ValueError:

privkey = RSA.importKey(pkey)
Esser420
  • 780
  • 1
  • 8
  • 19