【问题标题】:Create certificate for openssl_pkcs7_sign in php在php中为openssl_pkcs7_sign创建证书
【发布时间】:2023-03-13 19:29:01
【问题描述】:

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

在 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");
}
?>

提前感谢您的帮助。

编辑 1:

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

我用命令生成了证书文件

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

还是报错:

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

编辑 2:添加前置

也许它与“预先准备”有关?我真的不确定问题出在文件检索还是密钥本身。

【问题讨论】:

  • 第二个参数“signed.txt”是你想要的输出文件。
  • 不,问题是我遇到了错误。我不确定我是否以正确的方式生成了证书文件。我使用了 openssl req -x509 -days 365 -newkey rsa:1024 -keyout enc_key.pem -out selfcert.pem。我确实编辑了问题以使其更清楚。

标签: php openssl


【解决方案1】:

我自己解决了。问题是正确检索密钥。 所以对于遇到这个问题的每个人:

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

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2019-04-30
    相关资源
    最近更新 更多