= 4.0.6, PHP 5, PHP 7, PHP 8)openssl_pkcs7_encrypt — 加密一個(gè) S/MIME 消息說明openssl_pkcs7_encrypt( string $infile, string ">
(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)
openssl_pkcs7_encrypt — 加密一個(gè) S/MIME 消息
$infile
,$outfile
,$recipcerts
,$headers
,$flags
= 0,$cipherid
= OPENSSL_CIPHER_RC2_40
openssl_pkcs7_encrypt() 獲取文件名為infile
的文件內(nèi)容并使用 RC2 40位的密碼將之加密,以至于他們只能被預(yù)期的名為recipcerts
的接收者閱讀。
infile
outfile
recipcerts
一個(gè)單獨(dú)的X.509證書,或者一個(gè)X.509證書的數(shù)組。
headers
headers
是包含頭信息的數(shù)組,在被加密后將對(duì)數(shù)據(jù)進(jìn)行預(yù)處理。
headers
可以是以頭名為鍵值的關(guān)聯(lián)數(shù)組,也可以是一個(gè)索引數(shù)組,其中每個(gè)元素都包含一個(gè)單獨(dú)的標(biāo)題行
flags
flags
用來指定影響編碼過程的選項(xiàng) - 參見 PKCS7
常量.
cipherid
密碼常量之一。
成功時(shí)返回 true
, 或者在失敗時(shí)返回 false
。
示例 #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");
}
?>