Home  >  Article  >  php教程  >  PHP实现加密解密算法

PHP实现加密解密算法

WBOY
WBOYOriginal
2016-06-21 08:52:511354browse

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>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn