OpenSSL won't create private keys?

℡╲_俬逩灬. 提交于 2019-11-29 14:13:00

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;
?>

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.

Diego Favero

I Had the same problem.

And it is Windows problem's !!!

It won't happen on unix base systems.

And, If like me you use Windows only for Development, you use PuTTYGen to generate a key, and use it, static, in dev enviroment !

OR

OpenSSL not working on Windows, errors 0x02001003 0x2006D080 0x0E064002

use : openssl_pkey_export_to_file($result, 'privkey.pem', null, $config);

This worked for wampp/php 7.0.4

$privateKey = openssl_pkey_new(array(
'config' => 'C:/wamp/bin/php/php7.0.4/extras/ssl/openssl.cnf',
'private_key_bits' => 1024,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
 ));

// write private key to file
openssl_pkey_export_to_file($privateKey, 'private.key');

// generate public key from private key
$publicKey = openssl_pkey_get_details($privateKey);

// write public key to file
file_put_contents('public.key', $publicKey['key']);

// clear key
echo $privateKey;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!