PHP写的加密函数,支持私人密钥(详细介绍)

在开发PHP系统时,会员部分往往是一个必不可少的模块,而密码的处理又是不得不面对的问题,PHP 的 Mcrypt 加密库又需要额外设置,很多人都是直接使用md5()函数加密,这个方法的确安全,但是因为md5是不可逆加密,无法还原密码,因此也有一些不便之处,本文介绍加密函数支持私钥,用起来还是不错的.
代码如下:
PHP:

复制代码 代码如下:


<ol><li><div>&nbsp;</div>
</li>
<li><div><span><?php</span>
 </div>
</li>
<li><div><span>// 说明:PHP 写的加密函数,支持私人密钥 </span>
</div>
</li>
<li><div><span>// 整理:https://www.jb51.net </span>
</div>
</li>
<li><div>&nbsp;</div>
</li>
<li><div><span>function</span>
 keyED<span>(</span>
<span>$txt</span>
,<span>$encrypt_key</span>
<span>)</span>
   </div>
</li>
<li><div><span>{</span>
   </div>
</li>
<li><div>    <span>$encrypt_key</span>
 = <span>md5</span>
<span>(</span>
<span>$encrypt_key</span>
<span>)</span>
;   </div>
</li>
<li><div>    <span>$ctr</span>
=<span>0</span>
;   </div>
</li>
<li><div>    <span>$tmp</span>
 = <span>""</span>
;   </div>
</li>
<li><div>    <span>for</span>
 <span>(</span>
<span>$i</span>
=<span>0</span>
;<span>$i</span>
<strlen<span>(</span>
<span>$txt</span>
<span>)</span>
;<span>$i</span>
++<span>)</span>
   </div>
</li>
<li><div>    <span>{</span>
   </div>
</li>
<li><div>        <span>if</span>
 <span>(</span>
<span>$ctr</span>
==<span>strlen</span>
<span>(</span>
<span>$encrypt_key</span>
<span>)</span>
<span>)</span>
 <span>$ctr</span>
=<span>0</span>
;   </div>
</li>
<li><div>        <span>$tmp</span>
.= <span>substr</span>
<span>(</span>
<span>$txt</span>
,<span>$i</span>
,<span>1</span>
<span>)</span>
 ^ <span>substr</span>
<span>(</span>
<span>$encrypt_key</span>
,<span>$ctr</span>
,<span>1</span>
<span>)</span>
;   </div>
</li>
<li><div>        <span>$ctr</span>
++;   </div>
</li>
<li><div>    <span>}</span>
   </div>
</li>
<li><div>    <span>return</span>
 <span>$tmp</span>
;   </div>
</li>
<li><div><span>}</span>
   </div>
</li>
<li><div>&nbsp;</div>
</li>
<li><div><span>function</span>
 encrypt<span>(</span>
<span>$txt</span>
,<span>$key</span>
<span>)</span>
   </div>
</li>
<li><div><span>{</span>
   </div>
</li>
<li><div>    <span>srand</span>
<span>(</span>
<span>(</span>
double<span>)</span>
<span>microtime</span>
<span>(</span>
<span>)</span>
*<span>1000000</span>
<span>)</span>
;   </div>
</li>
<li><div>    <span>$encrypt_key</span>
 = <span>md5</span>
<span>(</span>
<span>rand</span>
<span>(</span>
<span>0</span>
,<span>32000</span>
<span>)</span>
<span>)</span>
;   </div>
</li>
<li><div>    <span>$ctr</span>
=<span>0</span>
;   </div>
</li>
<li><div>    <span>$tmp</span>
 = <span>""</span>
;   </div>
</li>
<li><div>    <span>for</span>
 <span>(</span>
<span>$i</span>
=<span>0</span>
;<span>$i</span>
<strlen<span>(</span>
<span>$txt</span>
<span>)</span>
;<span>$i</span>
++<span>)</span>
   </div>
</li>
<li><div>    <span>{</span>
   </div>
</li>
<li><div>        <span>if</span>
 <span>(</span>
<span>$ctr</span>
==<span>strlen</span>
<span>(</span>
<span>$encrypt_key</span>
<span>)</span>
<span>)</span>
 <span>$ctr</span>
=<span>0</span>
;   </div>
</li>
<li><div>        <span>$tmp</span>
.= <span>substr</span>
<span>(</span>
<span>$encrypt_key</span>
,<span>$ctr</span>
,<span>1</span>
<span>)</span>
 . <span>(</span>
<span>substr</span>
<span>(</span>
<span>$txt</span>
,<span>$i</span>
,<span>1</span>
<span>)</span>
 ^ <span>substr</span>
<span>(</span>
<span>$encrypt_key</span>
,<span>$ctr</span>
,<span>1</span>
<span>)</span>
<span>)</span>
;   </div>
</li>
<li><div>        <span>$ctr</span>
++;   </div>
</li>
<li><div>    <span>}</span>
   </div>
</li>
<li><div>    <span>return</span>
 keyED<span>(</span>
<span>$tmp</span>
,<span>$key</span>
<span>)</span>
; </div>
</li>
<li><div><span>}</span>
   </div>
</li>
<li><div>&nbsp;</div>
</li>
<li><div><span>function</span>
 decrypt<span>(</span>
<span>$txt</span>
,<span>$key</span>
<span>)</span>
   </div>
</li>
<li><div><span>{</span>
   </div>
</li>
<li><div>    <span>$txt</span>
 = keyED<span>(</span>
<span>$txt</span>
,<span>$key</span>
<span>)</span>
;   </div>
</li>
<li><div>    <span>$tmp</span>
 = <span>""</span>
;   </div>
</li>
<li><div>    <span>for</span>
 <span>(</span>
<span>$i</span>
=<span>0</span>
;<span>$i</span>
<strlen<span>(</span>
<span>$txt</span>
<span>)</span>
;<span>$i</span>
++<span>)</span>
   </div>
</li>
<li><div>    <span>{</span>
   </div>
</li>
<li><div>        <span>$md5</span>
 = <span>substr</span>
<span>(</span>
<span>$txt</span>
,<span>$i</span>
,<span>1</span>
<span>)</span>
;   </div>
</li>
<li><div>        <span>$i</span>
++;   </div>
</li>
<li><div>        <span>$tmp</span>
.= <span>(</span>
<span>substr</span>
<span>(</span>
<span>$txt</span>
,<span>$i</span>
,<span>1</span>
<span>)</span>
 ^ <span>$md5</span>
<span>)</span>
;   </div>
</li>
<li><div>    <span>}</span>
   </div>
</li>
<li><div>    <span>return</span>
 <span>$tmp</span>
;   </div>
</li>
<li><div><span>}</span>
  </div>
</li>
<li><div>&nbsp;</div>
</li>
<li><div><span>$key</span>
 = <span>"www.yitu.org"</span>
;   </div>
</li>
<li><div><span>$string</span>
 = <span>"我是加密字符"</span>
;   </div>
</li>
<li><div>&nbsp;</div>
</li>
<li><div><span>// encrypt $string, and store it in $enc_text   </span>
</div>
</li>
<li><div><span>$enc_text</span>
 = encrypt<span>(</span>
<span>$string</span>
,<span>$key</span>
<span>)</span>
;   </div>
</li>
<li><div>&nbsp;</div>
</li>
<li><div><span>// decrypt the encrypted text $enc_text, and store it in $dec_text   </span>
</div>
</li>
<li><div><span>$dec_text</span>
 = decrypt<span>(</span>
<span>$enc_text</span>
,<span>$key</span>
<span>)</span>
;   </div>
</li>
<li><div>&nbsp;</div>
</li>
<li><div><span>print</span>
 <span>"加密的 text : $enc_text <Br> "</span>
;   </div>
</li>
<li><div><span>print</span>
 <span>"解密的 text : $dec_text <Br> "</span>
;   </div>
</li>
<li><div><span>?></span>
 </div>
</li>
<li><div>&nbsp;</div>
</li>
</ol>


每一次加密后的结果是不一样的,大大加强了密码的安全性.

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/deb53849c31fdebc60df5dbc24fc8cfa.html