尝试通过简单的方式加载我的私钥时出现以下错误。这是我的代码。
public function loadPrivateKey($fileName, $password = null){
if(!is_file($fileName))
throw new SignException('Private key not found', SignException::KEY_NOT_FOUND);
$fileContent = file_get_contents($fileName);
if(!is_null($password))
$this->prvKey = openssl_get_privatekey($fileContent, $password);
else
$this->prvKey = openssl_get_privatekey($fileContent);
if(!empty(openssl_error_string()))
throw new SignException('OpenSSL Error: '.openssl_error_string());
if(!is_resource($this->prvKey))
throw new SignException('Private key is not resourse', SignException::EXTERNAL_ERROR);
}
openssl_error_string()
返回 error:2006D002:BIO routines:BIO_new_file:system lib
。
我在 php.ini
中使用 extension=php_openssl.dll
启用了 OpenSSL。
可能是什么问题?我该如何解决?
谢谢!
最佳答案
函数 openssl_get_privatekey()
是 openssl_pkey_get_private()
的别名.这个函数有两个参数;第一个是 URI 格式的文件名,或者是 PEM 格式的私钥的内容。第二个是密码。
您收到的错误表明尝试读取文件时出错;通常有问题的文件是 included in the error message ,所以您可能只在此处包含了部分错误。由于您没有使用 OpenSSL 读取文件,最有可能的罪魁祸首是 OpenSSL 配置文件;系统需要被告知在哪里寻找它。
- 右键单击“我的电脑”并进入“属性”
- 在“高级”选项卡上,单击“环境变量”按钮
- 在系统变量下创建一个新条目
- 变量名应该是“OPENSSL_CONF”
- 变量值应该是文件的完整路径
- 重启电脑
环境变量也可以从within your PHP code设置,尽管它需要添加到您的所有代码中,因此可能不是可取的。另外,如前所述,您可以直接从函数调用中打开 key 文件;这是我建议尝试的方法:
<?php
public function loadPrivateKey($fileName, $password = "") {
// I just used the value from my system here
putenv("OPENSSL_CONF=C:\\OpenSSL\\bin\\openssl.cfg");
if (!is_readable($fileName)) {
throw new SignException("Private key not found or not readable", SignException::KEY_NOT_FOUND);
}
$fileName = "file://$fileName";
$this->prvKey = openssl_get_privatekey($fileName, $password);
if (!empty(openssl_error_string())) {
throw new SignException("OpenSSL error: " . openssl_error_string());
}
if (!is_resource($this->prvKey)) {
throw new SignException("Private key is not resource", SignException::EXTERNAL_ERROR);
}
}
关于php - 错误 0x2006D002 :BIO routines:BIO_new_file:system lib with PHP on Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36280867/