无为清净楼资源网 Design By www.qnjia.com
在开发PHP系统时,会员部分往往是一个必不可少的模块,而密码的处理又是不得不面对的问题,PHP 的 Mcrypt 加密库又需要额外设置,很多人都是直接使用md5()函数加密,这个方法的确安全,但是因为md5是不可逆加密,无法还原密码,因此也有一些不便之处,本文介绍加密函数支持私钥,用起来还是不错的.
代码如下:
PHP:
复制代码 代码如下:
<ol><li class="li1"><div class="de1"> </div>
</li>
<li class="li1"><div class="de1"><span class="kw2"><?php</span>
</div>
</li>
<li class="li1"><div class="de1"><span class="co1">// 说明:PHP 写的加密函数,支持私人密钥 </span>
</div>
</li>
<li class="li1"><div class="de1"><span class="co1">// 整理:https://www.jb51.net </span>
</div>
</li>
<li class="li2"><div class="de2"> </div>
</li>
<li class="li1"><div class="de1"><span class="kw2">function</span>
keyED<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$encrypt_key</span>
<span class="br0">)</span>
</div>
</li>
<li class="li1"><div class="de1"><span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$encrypt_key</span>
= <span class="kw3">md5</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$ctr</span>
=<span class="nu0">0</span>
; </div>
</li>
<li class="li2"><div class="de2"> <span class="re0">$tmp</span>
= <span class="st0">""</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">for</span>
<span class="br0">(</span>
<span class="re0">$i</span>
=<span class="nu0">0</span>
;<span class="re0">$i</span>
<strlen<span class="br0">(</span>
<span class="re0">$txt</span>
<span class="br0">)</span>
;<span class="re0">$i</span>
++<span class="br0">)</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">if</span>
<span class="br0">(</span>
<span class="re0">$ctr</span>
==<span class="kw3">strlen</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
<span class="br0">)</span>
<span class="br0">)</span>
<span class="re0">$ctr</span>
=<span class="nu0">0</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$tmp</span>
.= <span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$i</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
^ <span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
,<span class="re0">$ctr</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li2"><div class="de2"> <span class="re0">$ctr</span>
++; </div>
</li>
<li class="li1"><div class="de1"> <span class="br0">}</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">return</span>
<span class="re0">$tmp</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="br0">}</span>
</div>
</li>
<li class="li1"><div class="de1"> </div>
</li>
<li class="li2"><div class="de2"><span class="kw2">function</span>
encrypt<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
</div>
</li>
<li class="li1"><div class="de1"><span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="kw3">srand</span>
<span class="br0">(</span>
<span class="br0">(</span>
double<span class="br0">)</span>
<span class="kw3">microtime</span>
<span class="br0">(</span>
<span class="br0">)</span>
*<span class="nu0">1000000</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$encrypt_key</span>
= <span class="kw3">md5</span>
<span class="br0">(</span>
<span class="kw3">rand</span>
<span class="br0">(</span>
<span class="nu0">0</span>
,<span class="nu0">32000</span>
<span class="br0">)</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$ctr</span>
=<span class="nu0">0</span>
; </div>
</li>
<li class="li2"><div class="de2"> <span class="re0">$tmp</span>
= <span class="st0">""</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">for</span>
<span class="br0">(</span>
<span class="re0">$i</span>
=<span class="nu0">0</span>
;<span class="re0">$i</span>
<strlen<span class="br0">(</span>
<span class="re0">$txt</span>
<span class="br0">)</span>
;<span class="re0">$i</span>
++<span class="br0">)</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">if</span>
<span class="br0">(</span>
<span class="re0">$ctr</span>
==<span class="kw3">strlen</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
<span class="br0">)</span>
<span class="br0">)</span>
<span class="re0">$ctr</span>
=<span class="nu0">0</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$tmp</span>
.= <span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
,<span class="re0">$ctr</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
. <span class="br0">(</span>
<span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$i</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
^ <span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
,<span class="re0">$ctr</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li2"><div class="de2"> <span class="re0">$ctr</span>
++; </div>
</li>
<li class="li1"><div class="de1"> <span class="br0">}</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">return</span>
keyED<span class="br0">(</span>
<span class="re0">$tmp</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="br0">}</span>
</div>
</li>
<li class="li1"><div class="de1"> </div>
</li>
<li class="li2"><div class="de2"><span class="kw2">function</span>
decrypt<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
</div>
</li>
<li class="li1"><div class="de1"><span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$txt</span>
= keyED<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$tmp</span>
= <span class="st0">""</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">for</span>
<span class="br0">(</span>
<span class="re0">$i</span>
=<span class="nu0">0</span>
;<span class="re0">$i</span>
<strlen<span class="br0">(</span>
<span class="re0">$txt</span>
<span class="br0">)</span>
;<span class="re0">$i</span>
++<span class="br0">)</span>
</div>
</li>
<li class="li2"><div class="de2"> <span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$md5</span>
= <span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$i</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$i</span>
++; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$tmp</span>
.= <span class="br0">(</span>
<span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$i</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
^ <span class="re0">$md5</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="br0">}</span>
</div>
</li>
<li class="li2"><div class="de2"> <span class="kw1">return</span>
<span class="re0">$tmp</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="br0">}</span>
</div>
</li>
<li class="li1"><div class="de1"> </div>
</li>
<li class="li1"><div class="de1"><span class="re0">$key</span>
= <span class="st0">"www.yitu.org"</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="re0">$string</span>
= <span class="st0">"我是加密字符"</span>
; </div>
</li>
<li class="li2"><div class="de2"> </div>
</li>
<li class="li1"><div class="de1"><span class="co1">// encrypt $string, and store it in $enc_text </span>
</div>
</li>
<li class="li1"><div class="de1"><span class="re0">$enc_text</span>
= encrypt<span class="br0">(</span>
<span class="re0">$string</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> </div>
</li>
<li class="li1"><div class="de1"><span class="co1">// decrypt the encrypted text $enc_text, and store it in $dec_text </span>
</div>
</li>
<li class="li2"><div class="de2"><span class="re0">$dec_text</span>
= decrypt<span class="br0">(</span>
<span class="re0">$enc_text</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> </div>
</li>
<li class="li1"><div class="de1"><span class="kw3">print</span>
<span class="st0">"加密的 text : $enc_text <Br> "</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="kw3">print</span>
<span class="st0">"解密的 text : $dec_text <Br> "</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="kw2">?></span>
</div>
</li>
<li class="li2"><div class="de2"> </div>
</li>
</ol>
每一次加密后的结果是不一样的,大大加强了密码的安全性.
代码如下:
PHP:
复制代码 代码如下:
<ol><li class="li1"><div class="de1"> </div>
</li>
<li class="li1"><div class="de1"><span class="kw2"><?php</span>
</div>
</li>
<li class="li1"><div class="de1"><span class="co1">// 说明:PHP 写的加密函数,支持私人密钥 </span>
</div>
</li>
<li class="li1"><div class="de1"><span class="co1">// 整理:https://www.jb51.net </span>
</div>
</li>
<li class="li2"><div class="de2"> </div>
</li>
<li class="li1"><div class="de1"><span class="kw2">function</span>
keyED<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$encrypt_key</span>
<span class="br0">)</span>
</div>
</li>
<li class="li1"><div class="de1"><span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$encrypt_key</span>
= <span class="kw3">md5</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$ctr</span>
=<span class="nu0">0</span>
; </div>
</li>
<li class="li2"><div class="de2"> <span class="re0">$tmp</span>
= <span class="st0">""</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">for</span>
<span class="br0">(</span>
<span class="re0">$i</span>
=<span class="nu0">0</span>
;<span class="re0">$i</span>
<strlen<span class="br0">(</span>
<span class="re0">$txt</span>
<span class="br0">)</span>
;<span class="re0">$i</span>
++<span class="br0">)</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">if</span>
<span class="br0">(</span>
<span class="re0">$ctr</span>
==<span class="kw3">strlen</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
<span class="br0">)</span>
<span class="br0">)</span>
<span class="re0">$ctr</span>
=<span class="nu0">0</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$tmp</span>
.= <span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$i</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
^ <span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
,<span class="re0">$ctr</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li2"><div class="de2"> <span class="re0">$ctr</span>
++; </div>
</li>
<li class="li1"><div class="de1"> <span class="br0">}</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">return</span>
<span class="re0">$tmp</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="br0">}</span>
</div>
</li>
<li class="li1"><div class="de1"> </div>
</li>
<li class="li2"><div class="de2"><span class="kw2">function</span>
encrypt<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
</div>
</li>
<li class="li1"><div class="de1"><span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="kw3">srand</span>
<span class="br0">(</span>
<span class="br0">(</span>
double<span class="br0">)</span>
<span class="kw3">microtime</span>
<span class="br0">(</span>
<span class="br0">)</span>
*<span class="nu0">1000000</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$encrypt_key</span>
= <span class="kw3">md5</span>
<span class="br0">(</span>
<span class="kw3">rand</span>
<span class="br0">(</span>
<span class="nu0">0</span>
,<span class="nu0">32000</span>
<span class="br0">)</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$ctr</span>
=<span class="nu0">0</span>
; </div>
</li>
<li class="li2"><div class="de2"> <span class="re0">$tmp</span>
= <span class="st0">""</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">for</span>
<span class="br0">(</span>
<span class="re0">$i</span>
=<span class="nu0">0</span>
;<span class="re0">$i</span>
<strlen<span class="br0">(</span>
<span class="re0">$txt</span>
<span class="br0">)</span>
;<span class="re0">$i</span>
++<span class="br0">)</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">if</span>
<span class="br0">(</span>
<span class="re0">$ctr</span>
==<span class="kw3">strlen</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
<span class="br0">)</span>
<span class="br0">)</span>
<span class="re0">$ctr</span>
=<span class="nu0">0</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$tmp</span>
.= <span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
,<span class="re0">$ctr</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
. <span class="br0">(</span>
<span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$i</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
^ <span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$encrypt_key</span>
,<span class="re0">$ctr</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li2"><div class="de2"> <span class="re0">$ctr</span>
++; </div>
</li>
<li class="li1"><div class="de1"> <span class="br0">}</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">return</span>
keyED<span class="br0">(</span>
<span class="re0">$tmp</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="br0">}</span>
</div>
</li>
<li class="li1"><div class="de1"> </div>
</li>
<li class="li2"><div class="de2"><span class="kw2">function</span>
decrypt<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
</div>
</li>
<li class="li1"><div class="de1"><span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$txt</span>
= keyED<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$tmp</span>
= <span class="st0">""</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="kw1">for</span>
<span class="br0">(</span>
<span class="re0">$i</span>
=<span class="nu0">0</span>
;<span class="re0">$i</span>
<strlen<span class="br0">(</span>
<span class="re0">$txt</span>
<span class="br0">)</span>
;<span class="re0">$i</span>
++<span class="br0">)</span>
</div>
</li>
<li class="li2"><div class="de2"> <span class="br0">{</span>
</div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$md5</span>
= <span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$i</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$i</span>
++; </div>
</li>
<li class="li1"><div class="de1"> <span class="re0">$tmp</span>
.= <span class="br0">(</span>
<span class="kw3">substr</span>
<span class="br0">(</span>
<span class="re0">$txt</span>
,<span class="re0">$i</span>
,<span class="nu0">1</span>
<span class="br0">)</span>
^ <span class="re0">$md5</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> <span class="br0">}</span>
</div>
</li>
<li class="li2"><div class="de2"> <span class="kw1">return</span>
<span class="re0">$tmp</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="br0">}</span>
</div>
</li>
<li class="li1"><div class="de1"> </div>
</li>
<li class="li1"><div class="de1"><span class="re0">$key</span>
= <span class="st0">"www.yitu.org"</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="re0">$string</span>
= <span class="st0">"我是加密字符"</span>
; </div>
</li>
<li class="li2"><div class="de2"> </div>
</li>
<li class="li1"><div class="de1"><span class="co1">// encrypt $string, and store it in $enc_text </span>
</div>
</li>
<li class="li1"><div class="de1"><span class="re0">$enc_text</span>
= encrypt<span class="br0">(</span>
<span class="re0">$string</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> </div>
</li>
<li class="li1"><div class="de1"><span class="co1">// decrypt the encrypted text $enc_text, and store it in $dec_text </span>
</div>
</li>
<li class="li2"><div class="de2"><span class="re0">$dec_text</span>
= decrypt<span class="br0">(</span>
<span class="re0">$enc_text</span>
,<span class="re0">$key</span>
<span class="br0">)</span>
; </div>
</li>
<li class="li1"><div class="de1"> </div>
</li>
<li class="li1"><div class="de1"><span class="kw3">print</span>
<span class="st0">"加密的 text : $enc_text <Br> "</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="kw3">print</span>
<span class="st0">"解密的 text : $dec_text <Br> "</span>
; </div>
</li>
<li class="li1"><div class="de1"><span class="kw2">?></span>
</div>
</li>
<li class="li2"><div class="de2"> </div>
</li>
</ol>
每一次加密后的结果是不一样的,大大加强了密码的安全性.
标签:
php,加密函数
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 炉石传说月初最强卡组有哪些 2024月初最强上分卡组推荐
- 狼人杀亮相原生鸿蒙之夜 假面科技强势登陆华为生态
- 12小时光线挑战!AI画质专家才是大平层首选
- 2024游戏IP报告:1~9月规模1960亿 68%用户愿为之付费
- 群星.2024-今夜一起为爱鼓掌电视剧原声带【相信音乐】【FLAC分轨】
- BIGFOUR.2013-大家利事【寰亚】【WAV+CUE】
- 李美凤.1992-情深透全情歌集【EMI百代】【WAV+CUE】
- 田震2024-《时光音乐会》[金峰][WAV+CUE]
- 群星《监听天碟3》[LECD]限量版[WAV+CUE]
- 心妤《声如夏花HQ》头版限量编号[WAV+CUE]
- 群星《摇滚五杰》[低速原抓WAV+CUE][1.1G]
- 群星 《2024好听新歌30》十倍音质 U盘音乐 [WAV+分轨]
- 群星《试音草原·女声篇》经典蒙古民歌[WAV+CUE][1G]
- 陈慧娴《永远是你的朋友》头版限量编号MQA-UHQCD2024[低速原抓WAV+CUE]
- 曼丽·女人三十《如果·爱》限量1:1母盘直刻[低速原抓WAV+CUE]