📄 mcrypt.examples.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Examples</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="mcrypt.ciphers.html">Mcrypt ciphers</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.mcrypt.html">Mcrypt Functions</a></div> <div class="up"><a href="book.mcrypt.html">Mcrypt</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Examples</h1> <p class="para"> Mcrypt can be used to encrypt and decrypt using the above mentioned ciphers. If you linked against libmcrypt-2.2.x, the four important mcrypt commands (<a href="function.mcrypt-cfb.html" class="function">mcrypt_cfb()</a>, <a href="function.mcrypt-cbc.html" class="function">mcrypt_cbc()</a>, <a href="function.mcrypt-ecb.html" class="function">mcrypt_ecb()</a>, and <a href="function.mcrypt-ofb.html" class="function">mcrypt_ofb()</a>) can operate in both modes which are named MCRYPT_ENCRYPT and MCRYPT_DECRYPT, respectively. <div class="example"> <p><b>Example #1 Encrypt an input value with TripleDES under 2.2.x in ECB mode</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$key </span><span style="color: #007700">= </span><span style="color: #DD0000">"this is a secret key"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$input </span><span style="color: #007700">= </span><span style="color: #DD0000">"Let us meet at 9 o'clock at the secret place."</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$encrypted_data </span><span style="color: #007700">= </span><span style="color: #0000BB">mcrypt_ecb </span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_3DES</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">MCRYPT_ENCRYPT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> This example will give you the encrypted data as a string in <i>$encrypted_data</i>. </p> <p class="para"> If you linked against libmcrypt 2.4.x or 2.5.x, these functions are still available, but it is recommended that you use the advanced functions. <div class="example"> <p><b>Example #2 Encrypt an input value with TripleDES under 2.4.x and higher in ECB mode</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> $key </span><span style="color: #007700">= </span><span style="color: #DD0000">"this is a secret key"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$input </span><span style="color: #007700">= </span><span style="color: #DD0000">"Let us meet at 9 o'clock at the secret place."</span><span style="color: #007700">;<br /><br /> </span><span style="color: #0000BB">$td </span><span style="color: #007700">= </span><span style="color: #0000BB">mcrypt_module_open</span><span style="color: #007700">(</span><span style="color: #DD0000">'tripledes'</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #DD0000">'ecb'</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$iv </span><span style="color: #007700">= </span><span style="color: #0000BB">mcrypt_create_iv </span><span style="color: #007700">(</span><span style="color: #0000BB">mcrypt_enc_get_iv_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">), </span><span style="color: #0000BB">MCRYPT_RAND</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">mcrypt_generic_init</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">$iv</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$encrypted_data </span><span style="color: #007700">= </span><span style="color: #0000BB">mcrypt_generic</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">, </span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">mcrypt_generic_deinit</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">mcrypt_module_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> This example will give you the encrypted data as a string in <i>$encrypted_data</i>. For a full example see <a href="function.mcrypt-module-open.html" class="function">mcrypt_module_open()</a>. </p></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mcrypt.ciphers.html">Mcrypt ciphers</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.mcrypt.html">Mcrypt Functions</a></div> <div class="up"><a href="book.mcrypt.html">Mcrypt</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -