By adding the following code and making a slight alteration to the PEM_write_PrivateKey arguments I got it working.
void init_openssl(void)
{
if (SSL_library_init()) {
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
RAND_load_file(_RAND_FILENAME, _RAND_LOADSIZE);
} else {
exit(EXIT_FAILURE);
}
}
And
if (!PEM_write_PrivateKey(priv_f, key_p,EVP_aes_256_cbc(),
(unsigned char*)passphrase,(int)strlen(passphrase), NULL, NULL)) {
fprintf(stderr, "Write private key failed\n");
handle_openssl_error();
return -1;
}