array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.openssl-pkcs7-encrypt.php', 1 => 'openssl_pkcs7_encrypt', ), 'up' => array ( 0 => 'ref.openssl.php', 1 => 'OpenSSL 函数', ), 'prev' => array ( 0 => 'function.openssl-pkcs7-decrypt.php', 1 => 'openssl_pkcs7_decrypt', ), 'next' => array ( 0 => 'function.openssl-pkcs7-read.php', 1 => 'openssl_pkcs7_read', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/openssl/functions/openssl-pkcs7-encrypt.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)
openssl_pkcs7_encrypt — 加密一个 S/MIME 消息
$input_filename
,$output_filename
,$certificate
,$headers
,$flags
= 0,$cipher_algo
= OPENSSL_CIPHER_AES_128_CBC
openssl_pkcs7_encrypt() 获取名为 input_filename
的文件内容并使用 RC2 40
位的密码将之加密,以至于他们只能被预期的名为 certificate
的接收者阅读。
版本 | 说明 |
---|---|
8.1.0 |
默认密码算法(cipher_algo )现在是 AES-128-CBC(OPENSSL_CIPHER_AES_128_CBC )。以前,使用
PKCS7/CMS (OPENSSL_CIPHER_RC2_40 )。
|
8.0.0 |
certificate 现在接受 OpenSSLCertificate
实例;之前接受类型 OpenSSL X.509 CSR 的 resource。
|
示例 #1 openssl_pkcs7_encrypt() 示例
<?php
// the message you want to encrypt and send to your secret agent
// in the field, known as nighthawk. You have his certificate
// in the file nighthawk.pem
$data = <<<EOD
Nighthawk,
Top secret, for your eyes only!
The enemy is closing in! Meet me at the cafe at 8.30am
to collect your forged passport!
HQ
EOD;
// load key
$key = file_get_contents("nighthawk.pem");
// save message to file
$fp = fopen("msg.txt", "w");
fwrite($fp, $data);
fclose($fp);
// encrypt it
if (openssl_pkcs7_encrypt("msg.txt", "enc.txt", $key,
array("To" => "nighthawk@example.com", // keyed syntax
"From: HQ <hq@example.com>", // indexed syntax
"Subject" => "Eyes only"))) {
// message encrypted - send it!
exec(ini_get("sendmail_path") . " < enc.txt");
}
?>