openssl_pkcs7_encrypt

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

openssl_pkcs7_encrypt加密一个 S/MIME 消息

说明

openssl_pkcs7_encrypt ( string $infile , string $outfile , mixed $recipcerts , array $headers [, int $flags = 0 [, int $cipherid = OPENSSL_CIPHER_RC2_40 ]] ) : bool

openssl_pkcs7_encrypt() 获取文件名为infile的文件内容并使用 RC2 40位的密码将之加密,以至于他们只能被预期的名为recipcerts的接收者阅读。

参数

infile

outfile

recipcerts

一个单独的X.509证书,或者一个X.509证书的数组。

headers

headers 是包含头信息的数组,在被加密后将对数据进行预处理。

headers 可以是以头名为键值的关联数组,也可以是一个索引数组,其中每个元素都包含一个单独的标题行

flags

flags用来指定影响编码过程的选项 - 参见 PKCS7 常量.

cipherid

密码常量之一。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

Example #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");
}
?>