Kbdman
Kbdman

Reputation: 136

How to encrypt and write RSA private key to file with PEM_Write_RSA_PrivateKey

The function:

int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
                                        unsigned char *kstr, int klen,
                                        pem_password_cb *cb, void *u);

The way I call the function :

PEM_write_RSAPrivateKey(pFile, pRSA,NULL,0,NULL,NULL,(void*)passphrase);

In openssl's documents,it says the last parameter is passphrase,but I find the context in the output file is not encrypted。

Upvotes: 2

Views: 1896

Answers (1)

Leśny Rumcajs
Leśny Rumcajs

Reputation: 2526

Obviously, because you set the cipher to NULL you won't get any encryption.

Go with something like:

PEM_write_RSAPrivateKey(pFile,pRSA,EVP_des_ede3_cbc(),passphrase, passphraseLength,NULL,NULL);

And of course change the cipher as you please, better option would be to use AES-128-CBC, but it's up to your specific goal and performance requirements.

Don't forget to initialize the OpenSSL library with something like OpenSSL_add_all_algorithms()

Upvotes: 1

Related Questions