PHP は暗号化および復号化アルゴリズムを実装します

WBOY
リリース: 2016-06-21 08:52:51
オリジナル
1375 人が閲覧しました

PHP は、次のコードに示すように、暗号化および復号化アルゴリズムを実装します。

<ol class="dp-c">
<li class="alt"><span><span><?php   </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>
ログイン後にコピー



関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート