function.mcrypt-encrypt.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 106 行
HTML
106 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Encrypts plaintext with given parameters</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-enc-self-test.html">mcrypt_enc_self_test</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mcrypt-generic-deinit.html">mcrypt_generic_deinit</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-encrypt" class="refentry"> <div class="refnamediv"> <h1 class="refname">mcrypt_encrypt</h1> <p class="verinfo">(PHP 4 >= 4.0.2, PHP 5)</p><p class="refpurpose"><span class="refname">mcrypt_encrypt</span> — <span class="dc-title">Encrypts plaintext with given parameters</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>mcrypt_encrypt</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$cipher</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$key</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$data</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">$iv</tt></span> ] )</div> <p class="para rdfs-comment"> <b>mcrypt_encrypt()</b> encrypts the data and returns the encrypted data. </p> <p class="para"> <i><tt class="parameter">Cipher</tt></i> is one of the MCRYPT_ciphername constants of the name of the algorithm as string. </p> <p class="para"> <i><tt class="parameter">Key</tt></i> is the key with which the data will be encrypted. If it's smaller that the required keysize, it is padded with '<i>\0</i>'. It is better not to use ASCII strings for keys. It is recommended to use the mhash functions to create a key from a string. </p> <p class="para"> <i><tt class="parameter">Data</tt></i> is the data that will be encrypted with the given cipher and mode. If the size of the data is not n * blocksize, the data will be padded with '<i>\0</i>'. The returned crypttext can be larger that the size of the data that is given by <i><tt class="parameter">data</tt></i>. </p> <p class="para"> <i><tt class="parameter">Mode</tt></i> is one of the MCRYPT_MODE_modename constants of one of "ecb", "cbc", "cfb", "ofb", "nofb" or "stream". </p> <p class="para"> The <i><tt class="parameter">IV</tt></i> parameter is used for the initialisation in CBC, CFB, OFB modes, and in some algorithms in STREAM mode. If you do not supply an IV, while it is needed for an algorithm, the function issues a warning and uses an IV with all bytes set to '<i>\0</i>'. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>mcrypt_encrypt()</b> Example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /> $iv_size </span><span style="color: #007700">= </span><span style="color: #0000BB">mcrypt_get_iv_size</span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_RIJNDAEL_256</span><span style="color: #007700">, </span><span style="color: #0000BB">MCRYPT_MODE_ECB</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 /> </span><span style="color: #0000BB">$key </span><span style="color: #007700">= </span><span style="color: #DD0000">"This is a very secret key"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$text </span><span style="color: #007700">= </span><span style="color: #DD0000">"Meet me at 11 o'clock behind the monument."</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /> </span><span style="color: #0000BB">$crypttext </span><span style="color: #007700">= </span><span style="color: #0000BB">mcrypt_encrypt</span><span style="color: #007700">(</span><span style="color: #0000BB">MCRYPT_RIJNDAEL_256</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #0000BB">MCRYPT_MODE_ECB</span><span style="color: #007700">, </span><span style="color: #0000BB">$iv</span><span style="color: #007700">);<br /> echo </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$crypttext</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 class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>4264</pre></div> </pre></div> </div> </p> <p class="para"> See also <a href="function.mcrypt-module-open.html" class="function">mcrypt_module_open()</a> for a more advanced API and an example. </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mcrypt-enc-self-test.html">mcrypt_enc_self_test</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mcrypt-generic-deinit.html">mcrypt_generic_deinit</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 + =
减小字号Ctrl + -
显示快捷键?