您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“PHP如何實現加密函數并支持私人密鑰”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“PHP如何實現加密函數并支持私人密鑰”這篇文章吧。
在開發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如何實現加密函數并支持私人密鑰”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。