在php中为openssl_pkcs7_sign创建证书 [英] Create certificate for openssl_pkcs7_sign in php

查看:342
本文介绍了在php中为openssl_pkcs7_sign创建证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在php中使用pkcs7签名电子邮件的文档,我必须使用先前生成的证书.

Regarding to the documentation on signing emails with pkcs7 in php I have to use a prior generated certificate.

在openssl中为该示例准确生成必要文件的命令是什么? http://www.php.net/manual/it/function .openssl-pkcs7-sign.php

What is the command to exactly generated the necessary files for this example in openssl? http://www.php.net/manual/it/function.openssl-pkcs7-sign.php

<?php
// the message you want to sign so that recipient can be sure it was you that
// sent it
$data = <<<EOD

You have my authorization to spend $10,000 on dinner expenses.

The CEO
EOD;
// save message to file
$fp = fopen("msg.txt", "w");
fwrite($fp, $data);
fclose($fp);
// encrypt it
if (openssl_pkcs7_sign("msg.txt", "signed.txt", "mycert.pem",
    array("file://mycert.pem", "mypassphrase"),
    array("To" => "joes@example.com", // keyed syntax
          "From: HQ <ceo@example.com>", // indexed syntax
          "Subject" => "Eyes only")
    )) {
    // message signed - send it!
    exec(ini_get("sendmail_path") . " < signed.txt");
}
?>

预先感谢您的帮助.

$prepend = "file:/";
openssl_pkcs7_sign($prepend . realpath(dirname(__FILE__)) . "/text.txt",
$prepend . realpath(dirname(__FILE__)) . "/enc.txt",
$prepend . realpath(dirname(__FILE__)) . "/selfcert.pem",
array($prepend . realpath(dirname(__FILE__)) . "/enc_key.pem", "123456"),
$headers);

我用命令生成了证书文件

I generated the cert file with the command

openssl req -x509 -days 365 -newkey rsa:1024 -keyout enc_key.pem -out selfcert.pem

仍然收到错误:

警告:openssl_pkcs7_sign():在...中获取私钥时出错.

Warning: openssl_pkcs7_sign(): error getting private key in ...

添加了前缀

也许与"preprend"有关?我真的不确定问题出在文件检索还是密钥本身.

Maybe it has something to do with the "preprend"? Im really not sure if the problem is either in the file retrieve or the key itself.

推荐答案

自行解决.问题是正确检索密钥. 因此,对于遇到这个问题的每个人:

Solved it on my own. The problem was retrieving the keys correctly. So for everyone coming onto that problem:

$prepend = "file://";
openssl_pkcs7_sign(realpath(dirname(__FILE__)) . "/text.txt",
        realpath(dirname(__FILE__)) . "/enc.txt",
        $prepend . realpath(dirname(__FILE__)) ."/selfcert.pem",
        array($prepend . realpath(dirname(__FILE__)) ."/enc_key.pem", "123456"), $headers);

这篇关于在php中为openssl_pkcs7_sign创建证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆