openssl_pkey_export and “cannot get key from parameter 1”

泄露秘密 提交于 2020-01-01 12:25:07

问题


I need to use openssl within my php project, so I created a test php page using openssl. However, I keep getting these errors and I am not sure why. openssl is enabled.

Warning: openssl_pkey_export() [function.openssl-pkey-export]: cannot get key from parameter 1 in C:\wamp\www\opensslsample\index.php on line 18

Warning: openssl_pkey_get_details() expects parameter 1 to be resource, boolean given in C:\wamp\www\opensslsample\index.php on line 21

<?php
 //echo phpinfo();

   $privateKey = openssl_pkey_new(array(
'private_key_bits' => 1024,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
));

openssl_pkey_export($privateKey, $privkey,"123");

$pubkey=openssl_pkey_get_details($privateKey);
$pubkey=$pubkey["key"];
?>

回答1:


This may help if you are on windows:

  1. Click on the START button
  2. Click on CONTROL PANEL
  3. Click on SYSTEM AND SECURITY
  4. Click on SYSTEM
  5. Click on ADVANCED SYSTEM SETTINGS
  6. Click on ENVIRONMENT VARIABLES
  7. Under "System Variables" click on "NEW"
  8. Enter the "Variable name" OPENSSL_CONF
  9. Enter the "Variable value". My is - C:\wamp\bin\apache\Apache2.2.17\conf\openssl.cnf
  10. Click "OK" and close all the windows and RESTART your computer.

The OPENSSL should be correctly working.




回答2:


Check openssl_error_string. My guess is that your openssl.cnf file is missing or something.

Alternatively, you could use phpseclib, a pure PHP RSA implementation, to generate keys. eg.

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

$rsa = new Crypt_RSA();

extract($rsa->createKey());

echo "$privatekey<br />$publickey";
?>



回答3:


The PHP needs to find your openssl.cnf. The best way to achieve this is to add the directory location of it in the PATH environment variable.




回答4:


I extract from phpseclib to fix it in jose-jwt lib and it worked, you need this several changes:

<?php

$config = array();
$config['config'] = dirname(__FILE__) . '/openssl.cnf';

$privateKey = openssl_pkey_new(array(
  'private_key_bits' => 1024,
  'private_key_type' => OPENSSL_KEYTYPE_RSA,
) + $config);

openssl_pkey_export($privateKey, $privkey, "123", $config);

And this minimalist config file:

# minimalist openssl.cnf file for use with phpseclib

HOME            = .
RANDFILE        = $ENV::HOME/.rnd

[ v3_ca ]

Called openssl.cnf. With all this it should work well.




回答5:


You can workaround by using ParagonIE\EasyRSA\KeyPair. Take a look at https://github.com/paragonie/EasyRSA.




回答6:


Warning: openssl_pkey_export(): cannot get key from parameter 1 in C:\xampp\htdocs\info.php on line 57

Warning: openssl_pkey_get_details() expects parameter 1 to be resource, bool given in C:\xampp\htdocs\info.php on line 60



来源:https://stackoverflow.com/questions/17272809/openssl-pkey-export-and-cannot-get-key-from-parameter-1

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