我目前正在使用PHP实现S / Mime解密。到目前为止我得到了什么:
$keys = array("public"=>$atm."/public-keys/".$usr.".smime",
"private"=>$atm."/private-keys/".$usr.".smime");
if(!file_exists($keys["public"])) die("Public Key not found");
if(!file_exists($keys["private"])) die("Private Key not found");
$public = file_get_contents($keys["public"]);
$private = file_get_contents($keys["private"]);
switch($_GET["debug"])
{
case "encrypt":
{
$outfile = realpath("demo-msg/out.txt");
$outfile_signed = realpath("demo-msg/out.signed.txt");
$infile = realpath("demo-msg/in.txt");
file_put_contents($infile,$msg);
$adddata = array("To" => "XXX", "From: Demo Name <XXX>", "Subject" => "Demo Subject");
if (openssl_pkcs7_encrypt($infile, $outfile, $public, $adddata))
{
//$info = file_get_contents($outfile);
echo "winenc & transfer<br>\n";
file_put_contents($infile, file_get_contents($outfile));
//if(openssl_pkcs7_sign($outfile,$outfile_signed,$public,$private,$adddata, PKCS7_BINARY)) echo "winsign";
//else echo "failsign";
}
else echo "Failed Encryption";
exit;
}
default:
{
$outfile2 = realpath("demo-msg/out2.txt");
$outfile = realpath("demo-msg/out.txt");
$infile = realpath("demo-msg/smime.p7m");
//$infile = realpath("demo-msg/in.txt");
if(openssl_pkcs7_verify($infile)) echo "verified<br>\n"; //tried: openssl_pkcs7_verify($infile,$PKCS7_DETACHED, tmpfile(), array(), array(), $outfile)
else die("invalid sig");
if(openssl_pkcs7_decrypt($infile, $outfile2, $public, $private)) //tried: openssl_pkcs7_decrypt($outfile, $outfile2, $public, $private)
{
echo "dec win:".file_get_contents($outfile2);
}
else echo "Oh oh! Decryption failed!";
exit;
}
}
此片段可以做什么:
现在,我想要解密签名的消息(因为它通常是一步)。问题:
尽管如此,我没有任何线索,第二次尝试可能出现什么问题。非常感谢你的帮助!
答案 0 :(得分:1)
对自己说。
我在剧本中犯的错误:
我误解的是方式,处理签名(并进行验证)。 我假设邮件已加密,然后签名。 可以那样,但很多工具,包括Office2010首先签署消息,然后加密它。这样,您无法在解密前检查签名,并且必须在解密后设置。
您可以在下面看到我的调试代码。当遇到这个问题时,这将帮助您解决问题。
$test = openssl_pkcs7_verify($infile, PKCS7_DETACHED ); //just to see that it doesn't work
echo "signature is ".$test."\n<br>".openssl_error_string();
$dec = openssl_pkcs7_decrypt($infile, $outfile, $public, $private);
echo "<br><br>\n\ndec is ".$dec."\n<br>".openssl_error_string()."\n<br>".file_get_contents($outfile);
$test = openssl_pkcs7_verify($outfile, PKCS7_DETACHED, $tmp, array(), $tmp, $outfile2 );
echo "<br><br>\n\nsignature2 is ".$test."\n<br>".openssl_error_string()."\n<br>".file_get_contents($outfile2);