DDR爱好者之家 Design By 杰米
在开发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>
每一次加密后的结果是不一样的,大大加强了密码的安全性.
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月27日
2024年11月27日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]