<!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>This function encrypts data</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mcrypt-generic-init.html">mcrypt_generic_init</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mcrypt-get-block-size.html">mcrypt_get_block_size</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.mcrypt-generic" class="refentry"> <div class="refnamediv"> <h1 class="refname">mcrypt_generic</h1> <p class="verinfo">(PHP 4 >= 4.0.2, PHP 5)</p><p class="refpurpose"><span class="refname">mcrypt_generic</span> — <span class="dc-title">This function encrypts data</span></p> </div> <div class="refsect1 description" id="refsect1-function.mcrypt-generic-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>mcrypt_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 encrypts data. The data is padded with "<em>\0</em>" to make sure the length of the data is n * blocksize. This function returns the encrypted data. Note that the length of the returned string can in fact be longer than the input, due to the padding of the data. </p> <p class="para"> If you want to store the encrypted data in a database make sure to store the entire string as returned by mcrypt_generic, or the string will not entirely decrypt properly. If your original string is 10 characters long and the block size is 8 (use <span class="function"><a href="function.mcrypt-enc-get-block-size.html" class="function">mcrypt_enc_get_block_size()</a></span> to determine the blocksize), you would need at least 16 characters in your database field. Note the string returned by <span class="function"><a href="function.mdecrypt-generic.html" class="function">mdecrypt_generic()</a></span> will be 16 characters as well...use rtrim($str, "\0") to remove the padding. </p> <p class="para"> If you are for example storing the data in a MySQL database remember that varchar fields automatically have trailing spaces removed during insertion. As encrypted data can end in a space (ASCII 32), the data will be damaged by this removal. Store data in a tinyblob/tinytext (or larger) field instead. </p> </div> <div class="refsect1 parameters" id="refsect1-function.mcrypt-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"> The encryption descriptor. </p> <p class="para"> The encryption 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> </dd> </dt> <dt> <span class="term"><em><code class="parameter">data</code></em></span> <dd> <p class="para"> The data to encrypt. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.mcrypt-generic-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the encrypted data. </p> </div> <div class="refsect1 seealso" id="refsect1-function.mcrypt-generic-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.mdecrypt-generic.html" class="function" rel="rdfs-seeAlso">mdecrypt_generic()</a> - Decrypts 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-generic-init.html">mcrypt_generic_init</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mcrypt-get-block-size.html">mcrypt_get_block_size</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>