Phpseclib在创建密钥时使用openssl


phpseclib is using openssl during create key?

我的印象是phpseclib不需要openssl,但是当我尝试下面的代码…

$rsa = new Crypt_RSA();
$key = $rsa->createKey();

我得到以下错误,如果它正在使用openssl函数。有点困惑。

Warning: openssl_pkey_export(): cannot get key from parameter 1 in /RSA.php on line 509  
Warning: openssl_pkey_get_details() expects parameter 1 to be resource, boolean given in /RSA.php on line 510  
Warning: array_values() expects parameter 1 to be /RSA.php on line 513  
Warning: call_user_func_array() expects parameter 2 to be array, null given in /RSA.php on line 513  
Warning: array_values() expects parameter 1 to be array, boolean given in /RSA.php on line 514
Warning: call_user_func_array() expects parameter 2 to be array, null given in /RSA.php on line 514 

phpseclib使用OpenSSL,如果它是可用的,但不是必需的。

短期修复

在顶端做define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_INTERNAL);

长期修复

看看这能为你做些什么会很有趣:

#
#-----[ OPEN ]------------------------------------------
#
Crypt/RSA.php
#
#-----[ FIND ]------------------------------------------
#
            $rsa = openssl_pkey_new(array(
                'private_key_bits' => $bits,
                'config' => dirname(__FILE__) . '/../openssl.cnf'
            ));
#
#-----[ AFTER, ADD ]------------------------------------
#
echo dirname(__FILE__) . "/../openssl.cnf'r'n";
echo file_exists(dirname(__FILE__) . '/../openssl.cnf') ? "exists'r'n" : "doesn't exist'r'n";

这不能解决问题,但它会给我们一些线索,让我们知道问题是什么。具体来说,我认为问题是这样的:

https://github.com/phpseclib/phpseclib/blob/0.3.1/phpseclib/Crypt/RSA.php L503

phpseclib检查在定义CRYPT_RSA_MODE_OPENSSL时是否定义了OpenSSL扩展,但在使用OpenSSL .cnf时不检查是否存在。可能是开发人员部分的疏忽,如果这就是导致您的问题的原因,那么永久的长期修复将是让phpseclib检查该文件的存在。