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

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

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

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

注意:路径一定不要搞错了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值