简体   繁体   中英

Import private rsa key fail

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM