OpenSSL won't create private keys?

I know that it has been a while and you may have already solved it but I think that my answer can be helpful for other people who faced the same problem (Like I did).

So if you take a look at the openssl_pkey_export() documentation you can find that it takes four different parameters. If we take a look at your second implementation we can see that you have only provided three.

openssl_pkey_export($res, $privKey, NULL);

This would be just as fine if only your server have the right environment variables for OPENSSL_CONF. Linking to the openssl.cnf file, which as quoted in the README-SSL.txt file that is downloaded with XAMPP is required to make use of CSR and key generation functions from PHP.

To use the CSR and key generation functions from PHP, you will need to install an openssl.cnf file. We have included a sample file that can be used for this purpose in this folder alongside this readme file.

For this reason just as you did for the openssl_pkey_new($config); you must create a associative array containing the config => '/path/to/openssl.cnf'. Just like this:

openssl_pkey_export($res, $privKey, NULL, $config);

I would like to also add that the path of openssl.cnf in my case is inside:

C:\xampp\php\extras\openssl\openssl.cnf

Inside here you will also be able to find a README-SSL.txt file which give a good explaination of how to configure OPENSSL.

Hope it helps. :)


Banged around my head with getting this to work on windows10 with xampp php 5.5. Finally figured tabone's answer above was in the right direction EXCEPT path format needed to be forward slashed!

C:/xampp/php/extras/openssl/openssl.cnf

Hope this helps someone.


I think you might have an easier time with phpseclib, a pure PHP RSA implementation. The following example will create a 1024-bit RSA private / public key:

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();

extract($rsa->createKey());

echo $privatekey . '<br/>' . $publickey;
?>