<!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>Opens the module of the algorithm and the mode to be used</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mcrypt-module-is-block-mode.html">mcrypt_module_is_block_mode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mcrypt-module-self-test.html">mcrypt_module_self_test</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-module-open" class="refentry"> <div class="refnamediv"> <h1 class="refname">mcrypt_module_open</h1> <p class="verinfo">(PHP 4 >= 4.0.2, PHP 5)</p><p class="refpurpose"><span class="refname">mcrypt_module_open</span> — <span class="dc-title">Opens the module of the algorithm and the mode to be used</span></p> </div> <div class="refsect1 description" id="refsect1-function.mcrypt-module-open-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>mcrypt_module_open</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$algorithm</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$algorithm_directory</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$mode</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$mode_directory</code></span> )</div> <p class="para rdfs-comment"> This function opens the module of the algorithm and the mode to be used. The name of the algorithm is specified in algorithm, e.g. <em>"twofish"</em> or is one of the <strong><code>MCRYPT_ciphername</code></strong> constants. The module is closed by calling <span class="function"><a href="function.mcrypt-module-close.html" class="function">mcrypt_module_close()</a></span>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.mcrypt-module-open-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">algorithm</code></em></span> <dd> <p class="para">One of the <strong><code>MCRYPT_ciphername</code></strong> constants, or the name of the algorithm as string.</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">algorithm_directory</code></em></span> <dd> <p class="para"> The <em><code class="parameter">algorithm_directory</code></em> parameter is used to locate the encryption module. When you supply a directory name, it is used. When you set it to an empty string (<em>""</em>), the value set by the <em>mcrypt.algorithms_dir</em> <var class="filename">php.ini</var> directive is used. When it is not set, the default directory that is used is the one that was compiled into libmcrypt (usually <var class="filename">/usr/local/lib/libmcrypt</var>). </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">mode</code></em></span> <dd> <p class="para">One of the <strong><code>MCRYPT_MODE_modename</code></strong> constants, or one of the following strings: "ecb", "cbc", "cfb", "ofb", "nofb" or "stream".</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">mode_directory</code></em></span> <dd> <p class="para"> The <em><code class="parameter">mode_directory</code></em> parameter is used to locate the encryption module. When you supply a directory name, it is used. When you set it to an empty string (<em>""</em>), the value set by the <em>mcrypt.modes_dir</em> <var class="filename">php.ini</var> directive is used. When it is not set, the default directory that is used is the one that was compiled-in into libmcrypt (usually <var class="filename">/usr/local/lib/libmcrypt</var>). </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.mcrypt-module-open-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Normally it returns an encryption descriptor, or <strong><code>FALSE</code></strong> on error. </p> </div> <div class="refsect1 examples" id="refsect1-function.mcrypt-module-open-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-850"> <p><strong>Example #1 <span class="function"><strong>mcrypt_module_open()</strong></span> Examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /> $td </span><span style="color: #007700">= </span><span style="color: #0000BB">mcrypt_module_open</span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_DES</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">MCRYPT_MODE_ECB</span><span style="color: #007700">, </span><span style="color: #DD0000">'/usr/lib/mcrypt-modes'</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">'rijndael-256'</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #DD0000">'ofb'</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> The first line in the example above will try to open the <em>DES</em> cipher from the default directory and the <em>ECB</em> mode from the directory <var class="filename">/usr/lib/mcrypt-modes</var>. The second example uses strings as name for the cipher and mode, this only works when the extension is linked against libmcrypt 2.4.x or 2.5.x. </p> <p class="para"> <div class="example" id="example-851"> <p><strong>Example #2 Using <span class="function"><strong>mcrypt_module_open()</strong></span> in encryption</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">/* Open the cipher */<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">'rijndael-256'</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #DD0000">'ofb'</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">/* Create the IV and determine the keysize length, use MCRYPT_RAND<br /> * on Windows instead */<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_DEV_RANDOM</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$ks </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 /><br /> </span><span style="color: #FF8000">/* Create key */<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">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'very secret key'</span><span style="color: #007700">), </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$ks</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">/* Intialize encryption */<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 /><br /> </span><span style="color: #FF8000">/* Encrypt data */<br /> </span><span style="color: #0000BB">$encrypted </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: #DD0000">'This is very important data'</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">/* Terminate encryption handler */<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">/* Initialize encryption module 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 /><br /> </span><span style="color: #FF8000">/* Decrypt encrypted string */<br /> </span><span style="color: #0000BB">$decrypted </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">$encrypted</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">/* Terminate decryption handle and close module */<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 /> </span><span style="color: #FF8000">/* Show string */<br /> </span><span style="color: #007700">echo </span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$decrypted</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.mcrypt-module-open-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.mcrypt-module-close.html" class="function" rel="rdfs-seeAlso">mcrypt_module_close()</a> - Closes the mcrypt module</span></li> <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.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-module-is-block-mode.html">mcrypt_module_is_block_mode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mcrypt-module-self-test.html">mcrypt_module_self_test</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>