PHP 加密 openssl_pkcs7_encrypt() 失败

发布于 2024-07-13 01:51:10 字数 1621 浏览 10 评论 0原文

我有这样的代码:

 $fp = fopen($unenc_path, "w");
 fwrite($fp, $msg);
 fclose($fp);

 $easy_access_emails = 'person@##.com';
 $headers =   "From: support@##.com <support@##.com>\n" . 
              "Reply-to: support@##.com\n" . 
              "Subject: " . $subject . "\n";

 $key = implode("", file("../newcert.pem"));

 $ArrayMessageProperties = explode("\n", $headers);

 $unenc_path = '..\\tmp\\'. preg_replace('/[^0-9]/','', microtime()) . rand(0,1000) . "msg.txt";
 $enc_path = '..\\tmp\\'. preg_replace('/[^0-9]/','', microtime()) . rand(0,1000) . "enc.txt";

 if (openssl_pkcs7_encrypt($unenc_path, $enc_path, $key, $ArrayMessageProperties))
 {
    $info = file_get_contents($enc_path);

    foreach ($easy_access_emails as $email)
    {
         mail($email, $subject, $info, $headers);
    }
} else {
    die("Failed Encryption");
}

它在我的本地开发环境(运行 LAMP 的 Macbook)上正常工作。 我将其移至 Windows 服务器进行测试,现在 openssl_pkcs7_encrypt 每次都会失败。 我认为这是一个权限问题,因为该函数需要写入 $enc_path; 但我已经使 Windows 服务器上的目录尽可能开放。 (将完全控制设置为几乎每个可能与之相关的用户/组。任何人都知道调试此问题的好方法吗?看起来该函数在失败时只是返回 false,而没有说明原因。

经过进一步调查,它看来脚本对目录具有写入权限。它写入 $unenc_path 没有问题,只是调用 openssl_pkcs7_encrypt 时失败。

另一个更新:我正在使用 filemon 进行观看。请求一些,我看到它打开并写入未加密的文件,但即使尝试写入加密的文件也没有输出,

我已经通过回显它来验证它正在加载。 。

另一个更新:再次观察 filemon,大约在它应该调用 openssq_pkcs7_encrypt 的时候,我看到一个条目,它正在寻找未加密的消息 Windows tmp 目录。

w3wp.exe:4172  C:\windows\system32\tmp\04277530010012336..msg.txt 
PATH NOT FOUND  Options: Open Access: Read

I have this code:

 $fp = fopen($unenc_path, "w");
 fwrite($fp, $msg);
 fclose($fp);

 $easy_access_emails = 'person@##.com';
 $headers =   "From: support@##.com <support@##.com>\n" . 
              "Reply-to: support@##.com\n" . 
              "Subject: " . $subject . "\n";

 $key = implode("", file("../newcert.pem"));

 $ArrayMessageProperties = explode("\n", $headers);

 $unenc_path = '..\\tmp\\'. preg_replace('/[^0-9]/','', microtime()) . rand(0,1000) . "msg.txt";
 $enc_path = '..\\tmp\\'. preg_replace('/[^0-9]/','', microtime()) . rand(0,1000) . "enc.txt";

 if (openssl_pkcs7_encrypt($unenc_path, $enc_path, $key, $ArrayMessageProperties))
 {
    $info = file_get_contents($enc_path);

    foreach ($easy_access_emails as $email)
    {
         mail($email, $subject, $info, $headers);
    }
} else {
    die("Failed Encryption");
}

Which works correctly on my local dev environment (Macbook running LAMP). I moved it to a windows server for testing and now openssl_pkcs7_encrypt fails every time. I assume this is a permissions issue, since the function needs to write to $enc_path; But I've made the directory on the windows server about as open as is possible. (set full control to just about every user / group that might have something to do with it. Anyone know a good method to debug this? It appears the function simply returns false when it fails with no indication as to why.

Upon further investigation it appears the script has write access the directory. It writes to the $unenc_path no problem, just fails on the call to openssl_pkcs7_encrypt.

Another update: I'm using filemon to watch the request some in and i see it opening and writing to the unencrypted file but there's no output for it even trying to write to the encrypted file.

Also added the line that loads the key. I've verified it's loading by echoing it out and it seems to be good.

Another update: Watching filemon again, around the time it should be calling openssq_pkcs7_encrypt I'm seeing an entry that it's looking for the unencrypted message in the windows tmp directory.

w3wp.exe:4172  C:\windows\system32\tmp\04277530010012336..msg.txt 
PATH NOT FOUND  Options: Open Access: Read

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给妤﹃绝世温柔 2024-07-20 01:51:10

结果 Windows / openssl_pkcs7_encrypt 不喜欢那个相对路径,在弄乱了正斜杠和反斜杠 (/. \\) 后给出了完整路径,稍微修复了它。

Turned out Windows / openssl_pkcs7_encrypt didn't like that relative path, giving a full path after messing with forward and backslashes (/. \\) for a bit fixed it.

盛装女皇 2024-07-20 01:51:10

您是否在代码中的某个位置加载 $key 中的密钥? 我在片段中看不到它

Are you loading the key in $key somewhere in your code? I can't see it on the snippet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文