<!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>Decrypts data</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mcrypt-ofb.html">mcrypt_ofb</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">Decrypts data</span></p> </div> <div class="refsect1 description" id="refsect1-function.mdecrypt-generic-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>mdecrypt_generic</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$td</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$data</code></span> )</div> <p class="para rdfs-comment"> This function decrypts data. Note that the length of the returned string can in fact be longer than the unencrypted string, due to the padding of the data. </p> </div> <div class="refsect1 parameters" id="refsect1-function.mdecrypt-generic-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">td</code></em></span> <dd> <p class="para"> An encryption descriptor returned by <span class="function"><a href="function.mcrypt-module-open.html" class="function">mcrypt_module_open()</a></span> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">data</code></em></span> <dd> <p class="para"> Encrypted data. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 examples" id="refsect1-function.mdecrypt-generic-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-853"> <p><strong>Example #1 <span class="function"><strong>mdecrypt_generic()</strong></span> Example</strong></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 example above 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 <span class="function"><a href="function.mcrypt-generic-init.html" class="function">mcrypt_generic_init()</a></span> before you try to decrypt the data. </p> <p class="para"> The decryption handle should always be initialized with <span class="function"><a href="function.mcrypt-generic-init.html" class="function">mcrypt_generic_init()</a></span> with a key and an IV before calling this function. Where the encryption is done, you should free the encryption buffers by calling <span class="function"><a href="function.mcrypt-generic-deinit.html" class="function">mcrypt_generic_deinit()</a></span>. See <span class="function"><a href="function.mcrypt-module-open.html" class="function">mcrypt_module_open()</a></span> for an example. </p> </div> <div class="refsect1 seealso" id="refsect1-function.mdecrypt-generic-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.mcrypt-generic.html" class="function" rel="rdfs-seeAlso">mcrypt_generic()</a> - This function encrypts data</span></li> <li class="member"> <span class="function"><a href="function.mcrypt-generic-init.html" class="function" rel="rdfs-seeAlso">mcrypt_generic_init()</a> - This function initializes all buffers needed for encryption</span></li> <li class="member"> <span class="function"><a href="function.mcrypt-generic-deinit.html" class="function" rel="rdfs-seeAlso">mcrypt_generic_deinit()</a> - This function deinitializes an encryption module</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mcrypt-ofb.html">mcrypt_ofb</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>