PHP 中openssl_pkey_get_private函数获取私钥返回 FALSE 的问题

来源:csdn博客 分类: 文章浏览史 发布时间:2020-09-18 12:08:43 最后更新:2020-09-18 浏览:1647
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-09-18 12:08:43

openssl_pkey_get_private ( mixed $key [, string $passphrase = “” ] ) : resource

参数
key

key 可以是如下密钥之一:
如下格式的字符串 file://path/to/file.pem。该文件必须包含 PEM 编码的证书或者私钥 (可能都包含了).
一个 PEM 格式的私钥。

passphrase

如果指定的密钥已被加密了 (受密码保护),可选参数 passphrase 是必须要的。

返回值

成功,返回真实的密钥资源标识符,失败,返回 FALSE .

使用方法

// 第一种方法 在路径前面拼接 file://  (但是这里要注意的是,路径和 file:// 是两个部分,
// 绝对路径下 /home/***.pem 最终得到的应该是 file:///home/***.pem,
// 注意这个地方 file: 后面跟着是三个斜线 )
$privateKey = openssl_pkey_get_private('file://'.$privateKey);

It’s actually “file://key.pem” when you want to give a relative path using unix systems. It will be three ‘/’ in case of absolute path (e.g “file:///home/username/…”). But this path consists of two ‘/’ originated from “file://” and one ‘/’ from the fact that home is a subfolder of the unix filesystem’s root directory ("/home/username/…"). This two part will be concatenated and you will get three ‘/’ characters following each other.
So you only have to concatenate “file://” with an existing path string in every case.

// 第二种方法 用 file_get_contents 读取 .pem 文件传入进去
$privateKey = openssl_pkey_get_private(file_get_contents($privateKey));

注意:路径一定不要搞错了

注意:路径一定不要搞错了

注意:路径一定不要搞错了
php技术微信