openssl_pkcs7_encrypt
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
openssl_pkcs7_encrypt — Encrypt an S/MIME message
Description
$infile
, string $outfile
, mixed $recipcerts
, array $headers
[, int $flags
= 0
[, int $cipherid
= OPENSSL_CIPHER_RC2_40
]] ) : bool
openssl_pkcs7_encrypt() takes the contents of the
file named infile
and encrypts them using an RC2
40-bit cipher so that they can only be read by the intended recipients
specified by recipcerts
.
Parameters
-
infile
-
-
outfile
-
-
recipcerts
-
Either a lone X.509 certificate, or an array of X.509 certificates.
-
headers
-
headers
is an array of headers that will be prepended to the data after it has been encrypted.headers
can be either an associative array keyed by header name, or an indexed array, where each element contains a single header line. -
flags
-
flags
can be used to specify options that affect the encoding process - see PKCS7 constants. -
cipherid
-
One of cipher constants.
Examples
Example #1 openssl_pkcs7_encrypt() example
<?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");
}
?>
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-openssl-pkcs7-encrypt.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.