<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Encrypt an S/MIME message</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.openssl-pkcs7-decrypt.html">openssl_pkcs7_decrypt</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.openssl-pkcs7-sign.html">openssl_pkcs7_sign</a></div> <div class="up"><a href="ref.openssl.html">OpenSSL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.openssl-pkcs7-encrypt" class="refentry"> <div class="refnamediv"> <h1 class="refname">openssl_pkcs7_encrypt</h1> <p class="verinfo">(PHP 4 >= 4.0.6, PHP 5)</p><p class="refpurpose"><span class="refname">openssl_pkcs7_encrypt</span> — <span class="dc-title">Encrypt an S/MIME message</span></p> </div> <div class="refsect1 description" id="refsect1-function.openssl-pkcs7-encrypt-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>openssl_pkcs7_encrypt</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$infile</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$outfile</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$recipcerts</code></span> , <span class="methodparam"><span class="type">array</span> <code class="parameter">$headers</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$cipherid</code><span class="initializer"> = OPENSSL_CIPHER_RC2_40</span></span> ]] )</div> <p class="para rdfs-comment"> <span class="function"><strong>openssl_pkcs7_encrypt()</strong></span> takes the contents of the file named <em><code class="parameter">infile</code></em> and encrypts them using an RC2 40-bit cipher so that they can only be read by the intended recipients specified by <em><code class="parameter">recipcerts</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.openssl-pkcs7-encrypt-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">infile</code></em></span> <dd> <p class="para"> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">outfile</code></em></span> <dd> <p class="para"> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">recipcerts</code></em></span> <dd> <p class="para"> Either a lone X.509 certificate, or an array of X.509 certificates. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">headers</code></em></span> <dd> <p class="para"> <em><code class="parameter">headers</code></em> is an array of headers that will be prepended to the data after it has been encrypted. </p> <p class="para"> <em><code class="parameter">headers</code></em> can be either an associative array keyed by header name, or an indexed array, where each element contains a single header line. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">flags</code></em></span> <dd> <p class="para"> <em><code class="parameter">flags</code></em> can be used to specify options that affect the encoding process - see <a href="openssl.pkcs7.flags.html" class="link">PKCS7 constants</a>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">cipherid</code></em></span> <dd> <p class="para"> One of <a href="openssl.ciphers.html" class="link">cipher constants</a>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.openssl-pkcs7-encrypt-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.openssl-pkcs7-encrypt-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.0.0</td> <td> The <em><code class="parameter">cipherid</code></em> parameter was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.openssl-pkcs7-encrypt-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-866"> <p><strong>Example #1 <span class="function"><strong>openssl_pkcs7_encrypt()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// the message you want to encrypt and send to your secret agent<br />// in the field, known as nighthawk. You have his certificate<br />// in the file nighthawk.pem<br /></span><span style="color: #0000BB">$data </span><span style="color: #007700">= <<<EOD<br /></span><span style="color: #DD0000">Nighthawk,<br /><br />Top secret, for your eyes only!<br /><br />The enemy is closing in! Meet me at the cafe at 8.30am<br />to collect your forged passport!<br /><br />HQ<br /></span><span style="color: #007700">EOD;<br /><br /></span><span style="color: #FF8000">// load key<br /></span><span style="color: #0000BB">$key </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">"nighthawk.pem"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// save message to file<br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"msg.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// encrypt it<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">openssl_pkcs7_encrypt</span><span style="color: #007700">(</span><span style="color: #DD0000">"msg.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"enc.txt"</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">,<br /> array(</span><span style="color: #DD0000">"To" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"nighthawk@example.com"</span><span style="color: #007700">, </span><span style="color: #FF8000">// keyed syntax<br /> </span><span style="color: #DD0000">"From: HQ <hq@example.com>"</span><span style="color: #007700">, </span><span style="color: #FF8000">// indexed syntax<br /> </span><span style="color: #DD0000">"Subject" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"Eyes only"</span><span style="color: #007700">))) {<br /> </span><span style="color: #FF8000">// message encrypted - send it!<br /> </span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #0000BB">ini_get</span><span style="color: #007700">(</span><span style="color: #DD0000">"sendmail_path"</span><span style="color: #007700">) . </span><span style="color: #DD0000">" < enc.txt"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.openssl-pkcs7-decrypt.html">openssl_pkcs7_decrypt</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.openssl-pkcs7-sign.html">openssl_pkcs7_sign</a></div> <div class="up"><a href="ref.openssl.html">OpenSSL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>