Rumah > php教程 > php手册 > teks badan

PHP实现加密解密算法

WBOY
Lepaskan: 2016-06-21 08:52:51
asal
1375 orang telah melayarinya

PHP实现加密解密的算法,如下代码:

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li><span><span class="keyword">class</span><span> Mcrypt   </span></span></li>
<li class="alt"><span>{   </span></li>
<li><span>    <span class="comment">/** </span> </span></li>
<li class="alt"><span><span class="comment">     * 解密 </span> </span></li>
<li><span><span class="comment">     *  </span> </span></li>
<li class="alt"><span><span class="comment">     * @param string $encryptedText 已加密字符串 </span> </span></li>
<li><span><span class="comment">     * @param string $key  密钥 </span> </span></li>
<li class="alt"><span><span class="comment">     * @return string </span> </span></li>
<li><span><span class="comment">     */</span><span>   </span></span></li>
<li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">function</span><span> _decrypt(</span><span class="vars">$encryptedText</span><span>,</span><span class="vars">$key</span><span> = null)   </span></span></li>
<li><span>    {   </span></li>
<li class="alt"><span>        <span class="vars">$key</span><span> = </span><span class="vars">$key</span><span> === null ? Config::get(</span><span class="string">'secret_key'</span><span>) : </span><span class="vars">$key</span><span>;   </span></span></li>
<li><span>        <span class="vars">$cryptText</span><span> = </span><span class="func">base64_decode</span><span>(</span><span class="vars">$encryptedText</span><span>);   </span></span></li>
<li class="alt"><span>        <span class="vars">$ivSize</span><span> = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);   </span></span></li>
<li><span>        <span class="vars">$iv</span><span> = mcrypt_create_iv(</span><span class="vars">$ivSize</span><span>, MCRYPT_RAND);   </span></span></li>
<li class="alt"><span>        <span class="vars">$decryptText</span><span> = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, </span><span class="vars">$key</span><span>, </span><span class="vars">$cryptText</span><span>, MCRYPT_MODE_ECB, </span><span class="vars">$iv</span><span>);   </span></span></li>
<li><span>        <span class="keyword">return</span><span> trim(</span><span class="vars">$decryptText</span><span>);   </span></span></li>
<li class="alt"><span>    }   </span></li>
<li><span>   </span></li>
<li class="alt"><span>    <span class="comment">/** </span> </span></li>
<li><span><span class="comment">     * 加密 </span> </span></li>
<li class="alt"><span><span class="comment">     * </span> </span></li>
<li><span><span class="comment">     * @param string $plainText 未加密字符串  </span> </span></li>
<li class="alt"><span><span class="comment">     * @param string $key        密钥 </span> </span></li>
<li><span><span class="comment">     */</span><span>   </span></span></li>
<li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">function</span><span> _encrypt(</span><span class="vars">$plainText</span><span>,</span><span class="vars">$key</span><span> = null)   </span></span></li>
<li><span>    {   </span></li>
<li class="alt"><span>        <span class="vars">$key</span><span> = </span><span class="vars">$key</span><span> === null ? Config::get(</span><span class="string">'secret_key'</span><span>) : </span><span class="vars">$key</span><span>;   </span></span></li>
<li><span>        <span class="vars">$ivSize</span><span> = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);   </span></span></li>
<li class="alt"><span>        <span class="vars">$iv</span><span> = mcrypt_create_iv(</span><span class="vars">$ivSize</span><span>, MCRYPT_RAND);   </span></span></li>
<li><span>        <span class="vars">$encryptText</span><span> = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, </span><span class="vars">$key</span><span>, </span><span class="vars">$plainText</span><span>, MCRYPT_MODE_ECB, </span><span class="vars">$iv</span><span>);   </span></span></li>
<li class="alt"><span>        <span class="keyword">return</span><span> trim(</span><span class="func">base64_encode</span><span>(</span><span class="vars">$encryptText</span><span>));   </span></span></li>
<li><span>    }   </span></li>
<li class="alt"><span>}   </span></li>
<li><span> </span></li>
<li class="alt"><span><span class="comment">//调用</span><span> </span></span></li>
<li><span><span class="keyword">class</span><span> Cookie </span><span class="keyword">extends</span><span> Mcrypt   </span></span></li>
<li class="alt"><span>{   </span></li>
<li><span>    <span class="comment">/** </span> </span></li>
<li class="alt"><span><span class="comment">     * 删除cookie </span> </span></li>
<li><span><span class="comment">     *  </span> </span></li>
<li class="alt"><span><span class="comment">     * @param array $args </span> </span></li>
<li><span><span class="comment">     * @return boolean </span> </span></li>
<li class="alt"><span><span class="comment">     */</span><span>   </span></span></li>
<li><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">function</span><span> del(</span><span class="vars">$args</span><span>)   </span></span></li>
<li class="alt"><span>    {   </span></li>
<li><span>        <span class="vars">$name</span><span> = </span><span class="vars">$args</span><span>[</span><span class="string">'name'</span><span>];   </span></span></li>
<li class="alt"><span>        <span class="vars">$domain</span><span> = isset(</span><span class="vars">$args</span><span>[</span><span class="string">'domain'</span><span>]) ? </span><span class="vars">$args</span><span>[</span><span class="string">'domain'</span><span>] : null;   </span></span></li>
<li><span>        <span class="keyword">return</span><span> isset(</span><span class="vars">$_COOKIE</span><span>[</span><span class="vars">$name</span><span>]) ? setcookie(</span><span class="vars">$name</span><span>, </span><span class="string">''</span><span>, time() - 86400, </span><span class="string">'/'</span><span>, </span><span class="vars">$domain</span><span>) : true;   </span></span></li>
<li class="alt"><span>    }   </span></li>
<li><span>       </span></li>
<li class="alt"><span>    <span class="comment">/** </span> </span></li>
<li><span><span class="comment">     * 得到指定cookie的值 </span> </span></li>
<li class="alt"><span><span class="comment">     *  </span> </span></li>
<li><span><span class="comment">     * @param string $name </span> </span></li>
<li class="alt"><span><span class="comment">     */</span><span>   </span></span></li>
<li><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">function</span><span> get(</span><span class="vars">$name</span><span>)   </span></span></li>
<li class="alt"><span>    {   </span></li>
<li><span>        <span class="keyword">return</span><span> isset(</span><span class="vars">$_COOKIE</span><span>[</span><span class="vars">$name</span><span>]) ? parent::_decrypt(</span><span class="vars">$_COOKIE</span><span>[</span><span class="vars">$name</span><span>]) : null;   </span></span></li>
<li class="alt"><span>    }   </span></li>
<li><span>       </span></li>
<li class="alt"><span>    <span class="comment">/** </span> </span></li>
<li><span><span class="comment">     * 设置cookie </span> </span></li>
<li class="alt"><span><span class="comment">     * </span> </span></li>
<li><span><span class="comment">     * @param array $args </span> </span></li>
<li class="alt"><span><span class="comment">     * @return boolean </span> </span></li>
<li><span><span class="comment">     */</span><span>   </span></span></li>
<li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">function</span><span> set(</span><span class="vars">$args</span><span>)   </span></span></li>
<li><span>    {   </span></li>
<li class="alt"><span>        <span class="vars">$name</span><span> = </span><span class="vars">$args</span><span>[</span><span class="string">'name'</span><span>];   </span></span></li>
<li><span>        <span class="vars">$value</span><span>= parent::_encrypt(</span><span class="vars">$args</span><span>[</span><span class="string">'value'</span><span>]);   </span></span></li>
<li class="alt"><span>        <span class="vars">$expire</span><span> = isset(</span><span class="vars">$args</span><span>[</span><span class="string">'expire'</span><span>]) ? </span><span class="vars">$args</span><span>[</span><span class="string">'expire'</span><span>] : null;   </span></span></li>
<li><span>        <span class="vars">$path</span><span> = isset(</span><span class="vars">$args</span><span>[</span><span class="string">'path'</span><span>]) ? </span><span class="vars">$args</span><span>[</span><span class="string">'path'</span><span>] : </span><span class="string">'/'</span><span>;   </span></span></li>
<li class="alt"><span>        <span class="vars">$domain</span><span> = isset(</span><span class="vars">$args</span><span>[</span><span class="string">'domain'</span><span>]) ? </span><span class="vars">$args</span><span>[</span><span class="string">'domain'</span><span>] : null;   </span></span></li>
<li><span>        <span class="vars">$secure</span><span> = isset(</span><span class="vars">$args</span><span>[</span><span class="string">'secure'</span><span>]) ? </span><span class="vars">$args</span><span>[</span><span class="string">'secure'</span><span>] : 0;   </span></span></li>
<li class="alt"><span>        <span class="keyword">return</span><span> setcookie(</span><span class="vars">$name</span><span>, </span><span class="vars">$value</span><span>, </span><span class="vars">$expire</span><span>, </span><span class="vars">$path</span><span>, </span><span class="vars">$domain</span><span>, </span><span class="vars">$secure</span><span>);   </span></span></li>
<li><span>    }   </span></li>
<li class="alt"><span>}  </span></li>
</ol>
Salin selepas log masuk



Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan