Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 1965

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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>Encryption Filters</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="filters.compression.html">Compression Filters</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="transports.html">List of Supported Socket Transports</a></div>
 <div class="up"><a href="filters.html">List of Available Filters</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="filters.encryption" class="section">
  <h2 class="title">Encryption Filters</h2>

  <p class="simpara">
   <em>mcrypt.*</em> and <em>mdecrypt.*</em>
   provide symmetric encryption and decryption using libmcrypt.
   Both sets of filters support the same algorithms available to
   <a href="ref.mcrypt.html" class="link">mcrypt extension</a> in the form of
   <em>mcrypt.ciphername</em> where <em><code class="parameter">ciphername</code></em>
   is the name of the cipher as it would be passed to
    <span class="function"><a href="function.mcrypt-module-open.html" class="function">mcrypt_module_open()</a></span>.
   The following five filter parameters are also available:
  </p>

  <p class="para">
   <table class="doctable table">
    <caption><strong>mcrypt filter parameters</strong></caption>
    
     <thead>
      <tr>
       <th>Parameter</th>
       <th>Required?</th>
       <th>Default</th>
       <th>Sample Values</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>mode</td>
       <td>Optional</td>
       <td>cbc</td>
       <td>cbc, cfb, ecb, nofb, ofb, stream</td>
      </tr>

      <tr>
       <td>algorithms_dir</td>
       <td>Optional</td>
       <td>ini_get(&#039;mcrypt.algorithms_dir&#039;)</td>
       <td>Location of algorithms modules</td>
      </tr>

      <tr>
       <td>modes_dir</td>
       <td>Optional</td>
       <td>ini_get(&#039;mcrypt.modes_dir&#039;)</td>
       <td>Location of modes modules</td>
      </tr>

      <tr>
       <td>iv</td>
       <td>Required</td>
       <td>N/A</td>
       <td>Typically 8, 16, or 32 bytes of binary data.  Depends on cipher</td>
      </tr>

      <tr>
       <td>key</td>
       <td>Required</td>
       <td>N/A</td>
       <td>Typically 8, 16, or 32 bytes of binary data.  Depends on cipher</td>
      </tr>

     </tbody>
    
   </table>

  </p>

  <div class="example" id="example-5639">
   <p><strong>Example #1 Encrypting file output using 3DES</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$passphrase&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'My&nbsp;secret'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Turn&nbsp;a&nbsp;human&nbsp;readable&nbsp;passphrase<br />&nbsp;*&nbsp;into&nbsp;a&nbsp;reproducable&nbsp;iv/key&nbsp;pair<br />&nbsp;*/<br /></span><span style="color: #0000BB">$iv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'iv'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'pass1'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;.&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'pass2'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">24</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$opts&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'iv'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'key'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'secret-file.enc'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'wb'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mcrypt.tripledes'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_FILTER_WRITE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$opts</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">,&nbsp;</span><span style="color: #DD0000">'Secret&nbsp;secret&nbsp;secret&nbsp;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 /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <div class="example" id="example-5640">
   <p><strong>Example #2 Reading an encrypted file</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$passphrase&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'My&nbsp;secret'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Turn&nbsp;a&nbsp;human&nbsp;readable&nbsp;passphrase<br />&nbsp;*&nbsp;into&nbsp;a&nbsp;reproducable&nbsp;iv/key&nbsp;pair<br />&nbsp;*/<br /></span><span style="color: #0000BB">$iv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'iv'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'pass1'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;.&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'pass2'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">24</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$opts&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'iv'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'key'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'secret-file.enc'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'rb'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mdecrypt.tripledes'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_FILTER_READ</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</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 />echo&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="filters.compression.html">Compression Filters</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="transports.html">List of Supported Socket Transports</a></div>
 <div class="up"><a href="filters.html">List of Available Filters</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>