<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.openssl.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'function.openssl-pkcs7-encrypt.php',
    1 => 'openssl_pkcs7_encrypt',
    2 => 'Encrypt an S/MIME message',
  ),
  'up' => 
  array (
    0 => 'ref.openssl.php',
    1 => 'OpenSSL Funzioni',
  ),
  'prev' => 
  array (
    0 => 'function.openssl-pkcs7-decrypt.php',
    1 => 'openssl_pkcs7_decrypt',
  ),
  'next' => 
  array (
    0 => 'function.openssl-pkcs7-read.php',
    1 => 'openssl_pkcs7_read',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/openssl/functions/openssl-pkcs7-encrypt.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.openssl-pkcs7-encrypt" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_pkcs7_encrypt</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">openssl_pkcs7_encrypt</span> &mdash; <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">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_pkcs7_encrypt</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$input_filename</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$output_filename</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="class.opensslcertificate.php" class="type OpenSSLCertificate">OpenSSLCertificate</a></span>|<span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$certificate</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.array.php" class="type array">array</a></span></span> <code class="parameter">$headers</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$cipher_algo</code><span class="initializer"> = <strong><code><a href="openssl.ciphers.php#constant.openssl-cipher-aes-128-cbc">OPENSSL_CIPHER_AES_128_CBC</a></code></strong></span></span><br>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_pkcs7_encrypt()</strong></span> takes the contents of the
   file named <code class="parameter">input_filename</code> and encrypts them using an RC2
   40-bit cipher so that they can only be read by the intended recipients
   specified by <code class="parameter">certificate</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-pkcs7-encrypt-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">input_filename</code></dt>
     <dd>
      <p class="para">
      </p>
     </dd>
    
    
     <dt><code class="parameter">output_filename</code></dt>
     <dd>
      <p class="para">
      </p>
     </dd>
    
    
     <dt><code class="parameter">certificate</code></dt>
     <dd>
      <p class="para">
       Either a lone X.509 certificate, or an array of X.509 certificates.
      </p>
     </dd>
    
    
     <dt><code class="parameter">headers</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">headers</code> is an array of headers that
       will be prepended to the data after it has been encrypted.
      </p>
      <p class="para">
       <code class="parameter">headers</code> 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><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">flags</code> can be used to specify options that affect
       the encoding process - see <a href="openssl.pkcs7.flags.php" class="link">PKCS7
       constants</a>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">cipher_algo</code></dt>
     <dd>
      <p class="para">
       One of <a href="openssl.ciphers.php" class="link">cipher constants</a>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-pkcs7-encrypt-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="para">
   Restituisce <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> in caso di successo, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> in caso di fallimento.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.openssl-pkcs7-encrypt-changelog">
  <h3 class="title">Log delle modifiche</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Versione</th>
      <th>Descrizione</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.1.0</td>
      <td>
       The default cipher algorithm (<code class="parameter">cipher_algo</code>) is now
       AES-128-CBC (<strong><code><a href="openssl.ciphers.php#constant.openssl-cipher-aes-128-cbc">OPENSSL_CIPHER_AES_128_CBC</a></code></strong>). Previously,
       PKCS7/CMS was used (<strong><code><a href="openssl.ciphers.php#constant.openssl-cipher-rc2-40">OPENSSL_CIPHER_RC2_40</a></code></strong>).
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">certificate</code> accepts an <span class="classname"><a href="class.opensslcertificate.php" class="classname">OpenSSLCertificate</a></span> instance now;
       previously, a <a href="language.types.resource.php" class="link">resource</a> of type <code class="literal">OpenSSL X.509 CSR</code> was accepted.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-pkcs7-encrypt-examples">
  <h3 class="title">Esempi</h3>
  <p class="para">
   <div class="example" id="example-1">
    <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">&lt;?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">= &lt;&lt;&lt;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">=&gt; </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 &lt;hq@example.com&gt;"</span><span style="color: #007700">, </span><span style="color: #FF8000">// indexed syntax<br />          </span><span style="color: #DD0000">"Subject" </span><span style="color: #007700">=&gt; </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">" &lt; enc.txt"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


</div><?php manual_footer($setup); ?>