PHP8 openssl_pkcs7_encrypt

2024-03-12 12:01 更新

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

openssl_pkcs7_encrypt — 加密一個(gè) S/MIME 消息

說(shuō)明

openssl_pkcs7_encrypt(
    string $input_filename,
    string $output_filename,
    OpenSSLCertificate|array|string $certificate,
    ?array $headers,
    int $flags = 0,
    int $cipher_algo = OPENSSL_CIPHER_AES_128_CBC
): bool

openssl_pkcs7_encrypt() 獲取名為 input_filename 的文件內(nèi)容并使用 RC2 40 位的密碼將之加密,以至于他們只能被預(yù)期的名為 certificate 的接收者閱讀。

參數(shù)

input_filename
output_filename
certificate

一個(gè)單獨(dú)的X.509證書(shū),或者一個(gè)X.509證書(shū)的數(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用來(lái)指定影響編碼過(guò)程的選項(xiàng) - 參見(jiàn) PKCS7 常量.

cipcipher_algoherid

密碼常量之一。

返回值 

成功時(shí)返回 true, 或者在失敗時(shí)返回 false。

更新日志 

版本說(shuō)明
8.1.0默認(rèn)密碼算法(cipher_algo)現(xiàn)在是 AES-128-CBC(OPENSSL_CIPHER_AES_128_CBC)。以前,使用 PKCS7/CMS (OPENSSL_CIPHER_RC2_40)。
8.0.0certificate 現(xiàn)在接受 OpenSSLCertificate 實(shí)例;之前接受類型 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");
}
?>


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)