📄 function.mdecrypt-generic.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Decrypt data</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="ref.mcrypt.html">Mcrypt Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.mhash.html">Mhash</a></div> <div class="up"><a href="ref.mcrypt.html">Mcrypt Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.mdecrypt-generic" class="refentry"> <div class="refnamediv"> <h1 class="refname">mdecrypt_generic</h1> <p class="verinfo">(PHP 4 >= 4.0.2, PHP 5)</p><p class="refpurpose"><span class="refname">mdecrypt_generic</span> — <span class="dc-title">Decrypt data</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><b><b>mdecrypt_generic</b></b></span> ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$td</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$data</tt></span> )</div> <p class="para rdfs-comment"> This function decrypts data. Note that the length of the returned string can in fact be longer then the unencrypted string, due to the padding of the data. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>mdecrypt_generic()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> </span><span style="color: #FF8000">/* Data */<br /> </span><span style="color: #0000BB">$key </span><span style="color: #007700">= </span><span style="color: #DD0000">'this is a very long key, even too long for the cipher'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$plain_text </span><span style="color: #007700">= </span><span style="color: #DD0000">'very important data'</span><span style="color: #007700">;<br /><br /> </span><span style="color: #FF8000">/* Open module, and create IV */<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">'des'</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">$key </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">mcrypt_enc_get_key_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">));<br /> </span><span style="color: #0000BB">$iv_size </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">);<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">$iv_size</span><span style="color: #007700">, </span><span style="color: #0000BB">MCRYPT_RAND</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">/* Initialize encryption handle */<br /> </span><span style="color: #007700">if (</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">) != -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /><br /> </span><span style="color: #FF8000">/* Encrypt data */<br /> </span><span style="color: #0000BB">$c_t </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">$plain_text</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 /><br /> </span><span style="color: #FF8000">/* Reinitialize buffers for decryption */<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">$p_t </span><span style="color: #007700">= </span><span style="color: #0000BB">mdecrypt_generic</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">, </span><span style="color: #0000BB">$c_t</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">/* Clean up */<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 /> }<br /><br /> if (</span><span style="color: #0000BB">strncmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$p_t</span><span style="color: #007700">, </span><span style="color: #0000BB">$plain_text</span><span style="color: #007700">, </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$plain_text</span><span style="color: #007700">)) == </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"ok\n"</span><span style="color: #007700">;<br /> } else {<br /> echo </span><span style="color: #DD0000">"error\n"</span><span style="color: #007700">;<br /> }<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> The above example shows how to check if the data before the encryption is the same as the data after the decryption. It is very important to reinitialize the encryption buffer with <a href="function.mcrypt-generic-init.html" class="function">mcrypt_generic_init()</a> before you try to decrypt the data. </p> <p class="para"> The decryption handle should always be initialized with <a href="function.mcrypt-generic-init.html" class="function">mcrypt_generic_init()</a> with a key and an IV before calling this function. Where the encryption is done, you should free the encryption buffers by calling <a href="function.mcrypt-generic-deinit.html" class="function">mcrypt_generic_deinit()</a>. See <a href="function.mcrypt-module-open.html" class="function">mcrypt_module_open()</a> for an example. </p> <p class="para"> See also <a href="function.mcrypt-generic.html" class="function">mcrypt_generic()</a>, <a href="function.mcrypt-generic-init.html" class="function">mcrypt_generic_init()</a>, and <a href="function.mcrypt-generic-deinit.html" class="function">mcrypt_generic_deinit()</a>. </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.mcrypt.html">Mcrypt Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.mhash.html">Mhash</a></div> <div class="up"><a href="ref.mcrypt.html">Mcrypt Functions</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 + -