filters.encryption.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 113 行

HTML
113
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Encryption Filters</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="filters.html">List of Available 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">   <i>mcrypt.*</i> and <i>mdecrypt.*</i>   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   <i>mcrypt.ciphername</i> where <i><tt class="parameter">ciphername</tt></i>   is the name of the cipher as it would be passed to   <a href="function.mcrypt-module-open.html" class="function">mcrypt_module_open()</a>.   The following five filter parameters are also available:  </p>  <p class="para">   <table border="5">    <caption><b>mcrypt filter parameters</b></caption>    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Parameter</th>       <th colspan="1">Required?</th>       <th colspan="1">Default</th>       <th colspan="1">Sample Values</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">mode</td>       <td colspan="1" rowspan="1" align="left">Optional</td>       <td colspan="1" rowspan="1" align="left">cbc</td>       <td colspan="1" rowspan="1" align="left">cbc, cfb, ecb, nofb, ofb, stream</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">algorithms_dir</td>       <td colspan="1" rowspan="1" align="left">Optional</td>       <td colspan="1" rowspan="1" align="left">ini_get(&#039;mcrypt.algorithms_dir&#039;)</td>       <td colspan="1" rowspan="1" align="left">Location of algorithms modules</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">modes_dir</td>       <td colspan="1" rowspan="1" align="left">Optional</td>       <td colspan="1" rowspan="1" align="left">ini_get(&#039;mcrypt.modes_dir&#039;)</td>       <td colspan="1" rowspan="1" align="left">Location of modes modules</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">iv</td>       <td colspan="1" rowspan="1" align="left">Required</td>       <td colspan="1" rowspan="1" align="left">N/A</td>       <td colspan="1" rowspan="1" align="left">Typically 8, 16, or 32 bytes of binary data.  Depends on cipher</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">key</td>       <td colspan="1" rowspan="1" align="left">Required</td>       <td colspan="1" rowspan="1" align="left">N/A</td>       <td colspan="1" rowspan="1" align="left">Typically 8, 16, or 32 bytes of binary data.  Depends on cipher</td>      </tr>     </tbody>    </colgroup>   </table>  </p>  <div class="example">   <p><b>Example #1 Encrypting file output using 3DES</b></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">'secert-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">   <p><b>Example #2 Reading an encrypted file</b></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">'secert-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_WRITE</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 style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="filters.html">List of Available 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>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?