📄 function.mcrypt-module-open.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Opens the module of the algorithm and the mode to be used</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="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"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><b><b>mcrypt_module_open</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$algorithm</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$algorithm_directory</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$mode</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$mode_directory</tt></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. "twofish" or is one of the MCRYPT_ciphername constants. The module is closed by calling <a href="function.mcrypt-module-close.html" class="function">mcrypt_module_close()</a>. Normally it returns an encryption descriptor, or <b><tt>FALSE</tt></b> on error. </p> <p class="para"> The <i><tt class="parameter">algorithm_directory</tt></i> and <i><tt class="parameter">mode_directory</tt></i> are used to locate the encryption modules. When you supply a directory name, it is used. When you set one of these to the empty string (""), the value set by the <i><tt class="parameter">mcrypt.algorithms_dir</tt></i> or <i><tt class="parameter">mcrypt.modes_dir</tt></i> ini-directive is used. When these are not set, the default directories that are used are the ones that were compiled in into libmcrypt (usually /usr/local/lib/libmcrypt). </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>mcrypt_module_open()</b> examples</b></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 DES cipher from the default directory and the EBC 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> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #2 Using <b>mcrypt_module_open()</b> in encryption</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">/* 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> <p class="para"> See also <a href="function.mcrypt-module-close.html" class="function">mcrypt_module_close()</a>, <a href="function.mcrypt-generic.html" class="function">mcrypt_generic()</a>, <a href="function.mdecrypt-generic.html" class="function">mdecrypt_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="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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -