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

前端之家收集整理的这篇文章主要介绍了PHP写的加密函数,支持私人密钥(详细介绍)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

代码如下:
<FONT style="COLOR: #ff0000">PHP:

<div class="codetitle"><a style="CURSOR: pointer" data="66041" class="copybut" id="copybut66041" onclick="doCopy('code66041')"> 代码如下:
<div class="codebody" id="code66041">
    <li class="li1"><div class="de1">


<li class="li1"><div class="de1"><span class="kw2"><?PHP


<li class="li1"><div class="de1"><span class="co1">// 说明:PHP 写的加密函数,支持私人密钥


<li class="li1"><div class="de1"><span class="co1">// 整理://www.jb51.cc


<li class="li2"><div class="de2">


<li class="li1"><div class="de1"><span class="kw2">function
keyED<span class="br0">(
<span class="re0">$txt
,<span class="re0">$encrypt_key
<span class="br0">)


<li class="li1"><div class="de1"><span class="br0">{


<li class="li1"><div class="de1"> <span class="re0">$encrypt_key
= <span class="kw3">md5
<span class="br0">(
<span class="re0">$encrypt_key
<span class="br0">)
;

<li class="li1"><div class="de1"> <span class="re0">$ctr
=<span class="nu0">0
;

<li class="li2"><div class="de2"> <span class="re0">$tmp
= <span class="st0">""
;

<li class="li1"><div class="de1"> <span class="kw1">for
<span class="br0">(
<span class="re0">$i
=<span class="nu0">0
;<span class="re0">$i
<strlen<span class="br0">(
<span class="re0">$txt
<span class="br0">)
;<span class="re0">$i
++<span class="br0">)


<li class="li1"><div class="de1"> <span class="br0">{


<li class="li1"><div class="de1"> <span class="kw1">if
<span class="br0">(
<span class="re0">$ctr
==<span class="kw3">strlen
<span class="br0">(
<span class="re0">$encrypt_key
<span class="br0">)
<span class="br0">)
<span class="re0">$ctr
=<span class="nu0">0
;

<li class="li1"><div class="de1"> <span class="re0">$tmp
.= <span class="kw3">substr
<span class="br0">(
<span class="re0">$txt
,<span class="re0">$i
,<span class="nu0">1
<span class="br0">)
^ <span class="kw3">substr
<span class="br0">(
<span class="re0">$encrypt_key
,<span class="re0">$ctr
,<span class="nu0">1
<span class="br0">)
;

<li class="li2"><div class="de2"> <span class="re0">$ctr
++;

<li class="li1"><div class="de1"> <span class="br0">}


<li class="li1"><div class="de1"> <span class="kw1">return
<span class="re0">$tmp
;

<li class="li1"><div class="de1"><span class="br0">}


<li class="li1"><div class="de1">

<li class="li2"><div class="de2"><span class="kw2">function
encrypt<span class="br0">(
<span class="re0">$txt
,<span class="re0">$key
<span class="br0">)


<li class="li1"><div class="de1"><span class="br0">{


<li class="li1"><div class="de1"> <span class="kw3">srand
<span class="br0">(
<span class="br0">(
double<span class="br0">)
<span class="kw3">microtime
<span class="br0">(
<span class="br0">)
*<span class="nu0">1000000
<span class="br0">)
;

<li class="li1"><div class="de1"> <span class="re0">$encrypt_key
= <span class="kw3">md5
<span class="br0">(
<span class="kw3">rand
<span class="br0">(
<span class="nu0">0
,<span class="nu0">32000
<span class="br0">)
<span class="br0">)
;

<li class="li1"><div class="de1"> <span class="re0">$ctr
=<span class="nu0">0
;

<li class="li2"><div class="de2"> <span class="re0">$tmp
= <span class="st0">""
;

<li class="li1"><div class="de1"> <span class="kw1">for
<span class="br0">(
<span class="re0">$i
=<span class="nu0">0
;<span class="re0">$i
<strlen<span class="br0">(
<span class="re0">$txt
<span class="br0">)
;<span class="re0">$i
++<span class="br0">)


<li class="li1"><div class="de1"> <span class="br0">{


<li class="li1"><div class="de1"> <span class="kw1">if
<span class="br0">(
<span class="re0">$ctr
==<span class="kw3">strlen
<span class="br0">(
<span class="re0">$encrypt_key
<span class="br0">)
<span class="br0">)
<span class="re0">$ctr
=<span class="nu0">0
;

<li class="li1"><div class="de1"> <span class="re0">$tmp
.= <span class="kw3">substr
<span class="br0">(
<span class="re0">$encrypt_key
,<span class="nu0">1
<span class="br0">)
. <span class="br0">(
<span class="kw3">substr
<span class="br0">(
<span class="re0">$txt
,<span class="nu0">1
<span class="br0">)
<span class="br0">)
;

<li class="li2"><div class="de2"> <span class="re0">$ctr
++;

<li class="li1"><div class="de1"> <span class="br0">}


<li class="li1"><div class="de1"> <span class="kw1">return
keyED<span class="br0">(
<span class="re0">$tmp
,<span class="re0">$key
<span class="br0">)
;

<li class="li1"><div class="de1"><span class="br0">}


<li class="li1"><div class="de1">

<li class="li2"><div class="de2"><span class="kw2">function
decrypt<span class="br0">(
<span class="re0">$txt
,<span class="re0">$key
<span class="br0">)


<li class="li1"><div class="de1"><span class="br0">{


<li class="li1"><div class="de1"> <span class="re0">$txt
= keyED<span class="br0">(
<span class="re0">$txt
,<span class="re0">$key
<span class="br0">)
;

<li class="li1"><div class="de1"> <span class="re0">$tmp
= <span class="st0">""
;

<li class="li1"><div class="de1"> <span class="kw1">for
<span class="br0">(
<span class="re0">$i
=<span class="nu0">0
;<span class="re0">$i
<strlen<span class="br0">(
<span class="re0">$txt
<span class="br0">)
;<span class="re0">$i
++<span class="br0">)


<li class="li2"><div class="de2"> <span class="br0">{


<li class="li1"><div class="de1"> <span class="re0">$md5
= <span class="kw3">substr
<span class="br0">(
<span class="re0">$txt
,<span class="nu0">1
<span class="br0">)
;

<li class="li1"><div class="de1"> <span class="re0">$i
++;

<li class="li1"><div class="de1"> <span class="re0">$tmp
.= <span class="br0">(
<span class="kw3">substr
<span class="br0">(
<span class="re0">$txt
,<span class="nu0">1
<span class="br0">)
^ <span class="re0">$md5
<span class="br0">)
;

<li class="li1"><div class="de1"> <span class="br0">}


<li class="li2"><div class="de2"> <span class="kw1">return
<span class="re0">$tmp
;

<li class="li1"><div class="de1"><span class="br0">}


<li class="li1"><div class="de1">

<li class="li1"><div class="de1"><span class="re0">$key
= <span class="st0">"www.yitu.org"
;

<li class="li1"><div class="de1"><span class="re0">$string
= <span class="st0">"我是加密字符"
;

<li class="li2"><div class="de2">

<li class="li1"><div class="de1"><span class="co1">// encrypt $string,and store it in $enc_text


<li class="li1"><div class="de1"><span class="re0">$enc_text
= encrypt<span class="br0">(
<span class="re0">$string
,<span class="re0">$key
<span class="br0">)
;

<li class="li1"><div class="de1">

<li class="li1"><div class="de1"><span class="co1">// decrypt the encrypted text $enc_text,and store it in $dec_text


<li class="li2"><div class="de2"><span class="re0">$dec_text
= decrypt<span class="br0">(
<span class="re0">$enc_text
,<span class="re0">$key
<span class="br0">)
;

<li class="li1"><div class="de1">

<li class="li1"><div class="de1"><span class="kw3">print
<span class="st0">"加密的 text : $enc_text
"
;

<li class="li1"><div class="de1"><span class="kw3">print
<span class="st0">"解密的 text : $dec_text
"
;

<li class="li1"><div class="de1"><span class="kw2">?>


<li class="li2"><div class="de2">



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

原文链接:https://www.f2er.com/php/26334.html
php加密函数

猜你在找的PHP相关文章