【问题标题】:openssl_pkey_export and "cannot get key from parameter 1"openssl_pkey_export 和“无法从参数 1 获取密钥”
【发布时间】:2013-06-20 19:15:34
【问题描述】:

我需要在我的 php 项目中使用 openssl,所以我使用 openssl 创建了一个测试 php 页面。但是,我不断收到这些错误,我不知道为什么。 openssl 已启用。

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

警告:openssl_pkey_get_details() 期望参数 1 是资源,布尔值在第 21 行的 C:\wamp\www\opensslsample\index.php 中给出

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

【问题讨论】:

标签: php openssl rsa


【解决方案1】:

如果您在 Windows 上,这可能会有所帮助:

  1. 点击开始按钮
  2. 点击控制面板
  3. 点击系统和安全
  4. 点击系统
  5. 点击高级系统设置
  6. 点击环境变量
  7. 在“系统变量”下点击“新建”
  8. 输入“变量名”OPENSSL_CONF
  9. 输入“变量值”。我的是-C:\wamp\bin\apache\Apache2.2.17\conf\openssl.cnf
  10. 单击“确定”并关闭所有窗口并重新启动计算机。

OPENSSL 应该可以正常工作。

【讨论】:

  • 第十步很关键。我尝试了很多在 stackoverflow 和其他博客上找到的解决方案,但没有一个提到您需要重新启动计算机。我花了 3 个小时尝试完成这项工作,但只有在我重新启动电脑后才能工作。
  • @Ansari 成功了!我在windows10上使用内置web服务器的php,所以我在系统变量中添加了以下内容:c:\php\extras\ssl\openssl.cnf,然后我重新启动了windows,就这样,谢谢。
【解决方案2】:

检查openssl_error_string。我的猜测是您的 openssl.cnf 文件丢失或其他原因。

或者,您可以使用phpseclib, a pure PHP RSA implementation 来生成密钥。例如。

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

$rsa = new Crypt_RSA();

extract($rsa->createKey());

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

【讨论】:

    【解决方案3】:

    我从phpseclib 中提取以在jose-jwt lib 中修复它并且它有效,您需要进行一些更改:

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

    还有这个极简的配置文件:

    # minimalist openssl.cnf file for use with phpseclib
    
    HOME            = .
    RANDFILE        = $ENV::HOME/.rnd
    
    [ v3_ca ]
    

    呼叫openssl.cnf。有了这一切,它应该可以很好地工作。

    【讨论】:

      【解决方案4】:

      PHP 需要找到您的 openssl.cnf。实现这一点的最佳方法是在 PATH 环境变量中添加它的目录位置。

      【讨论】:

        【解决方案5】:

        您可以使用 ParagonIE\EasyRSA\KeyPair 来解决问题。看看https://github.com/paragonie/EasyRSA。

        【讨论】:

          猜你喜欢
          • 2010-11-22
          • 2017-09-29
          • 2020-11-21
          • 2018-02-03
          • 2017-07-02
          • 2012-10-25
          • 2013-07-03
          • 2014-08-16
          • 1970-01-01
          相关资源
          最近更新 更多