我想使用PHP / OpenSSL与其他商家交换数据。每个企业都会创建公钥/私钥并发布公钥。然后,我编写代码来管理所有这些。这是PHP中的代码(主要来自php.net):
<?php
$data = "secret message";
$key = file_get_contents("bus1.pub");
// get temp file w/ write access
$plaintextfile = tempnam(sys_get_temp_dir(), 'abc');
$ciphertextfile = tempnam(sys_get_temp_dir(), 'abc');
$fp = fopen($plaintextfile, "w");
fwrite($fp, $data);
fclose($fp);
// encrypt it
if (openssl_pkcs7_encrypt($plaintextfile, $ciphertextfile, $key,
array("To" => "nighthawk@example.com", // keyed syntax
"From: HQ <hq@example.com>", // indexed syntax
"Subject" => "Eyes only"))) {
echo "encryption ok<br>";
} else
echo "failure<br>";
?>
然而,我收到错误(失败)。我怀疑我没有使用OpenSSL正确生成密钥。请帮助我们如何正确生成密钥,以便上面的PHP函数可以读取它们。
这就是我的尝试:
openssl genrsa -out bus1.pem 2048
openssl rsa -in bus1.pem -pubout > bus1.pub
答案 0 :(得分:0)
行。我弄清楚了这个错误。感谢@towr指出调试工具。对于其他读者,解决方案是按如下方式生成证书。每个企业都需要这样做,保留私钥并发布证书:
openssl genrsa -out business1.pass.key 2048
openssl rsa -in business1.pass.key -out business1.key
openssl req -new -key business1.key -out business1.csr
openssl x509 -req -days 3650 -in business1.csr -signkey business1.key -out business1.crt