php p12,使用PHP和OpenSSL将P12转换为PEM

这段代码演示了如何在PHP中使用openssl_pkcs12_read函数读取PKCS12证书文件,并将私钥导出为PEM格式。通过设置$password$为文件的密码(如果有的话),可以成功读取证书内容并进行转换。如果需要,可以使用openssl_pkey_export函数将私钥导出,并指定新的密码。在处理过程中,建议使用proc_open代替system以更好地捕获错误信息。
摘要由CSDN通过智能技术生成

$filename = 'apns-dev-cert.p12';

$password = '...';

$results = array();

$worked = openssl_pkcs12_read(file_get_contents($filename), $results, $password));

if($worked) {

echo '

', print_r($results, true), '
';

} else {

echo openssl_error_string();

}

请尝试运行此代码段.将$password设置为打开文件所需的密码.如果没有密码,请将其设置为null.我不相信你的openssl命令需要一个.

您应该使用所需的私钥输出,可能在$results [‘pkey’]中.

如果您在那里看到您的私钥,那么您可以将其传递给openssl_pkey_export以获得PEM格式,然后您可以将其写入文件:

$new_password = null;

$result = null;

$worked = openssl_pkey_export($results['pkey'], $result, $new_password);

if($worked) {

echo "

It worked!  Your new pkey is:\n", $result, '
';

} else {

echo openssl_error_string();

}

如果需要,将$new_password设置为您想要的pkey密码.

根据我在各种文档页面上阅读的内容,这应该适合您.

如果你真的想通过shelling out继续使用openssl命令,请考虑使用proc_open而不是system,这样你就可以正确捕获错误消息.

OpenSSL也可能正在尝试读取配置文件,并且没有权限这样做,尽管它应该给你一个错误.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值