Andrew
Andrew

Reputation: 71

failed to read private key from .pem file in c

I created mykey.pem using command

"openssl genrsa -out mykey.pem 1024"

and then I separate public key using command

'openssl rsa -in key.pem -pubout -out pubkey.pem'

I am reading private key using function,

PEM_read_RSAPrivateKey(fp,NULL,NULL,NULL)

But I could not retrieve the private key.

Do i have to get rid of headers like 'Begin RSA private key' and 'End RSA private key'? //Which i tried but didn't work

Do I have to send any other value to the PEM_read_RSAPrivateKey function?

or use some other function to retrieve the private key?

Upvotes: 1

Views: 4062

Answers (2)

Kunjan
Kunjan

Reputation: 354

if you are still not able to retrieve the private key using

PEM_read_RSAPrivateKey()

even after supplying the passphrase, a reason could be that you have not initialised the OpenSSL library properly, try adding

OpenSSL_add_all_algorithms();
OpenSSL_add_all_ciphers();
OpenSSL_add_all_digests();

before you call

PEM_read_RSAPrivateKey()

Upvotes: 2

doptimusprime
doptimusprime

Reputation: 9395

You do not need to get rid of headers from the file. However, if the file is password protected, you need to pass the password into function PEM_read_RSAPrivateKey.

You can also see PEM_read_PrivateKey. Other functions are listed here.

If the fp points to a file with RSA private key in PEM form and without any password, then it should succeed.

Upvotes: 0

Related Questions