PHP代码加密解密方法

时间:2009-03-27 01:06:39   来源:第二电脑网上收集  作者:第二电脑网

  第二电脑网导读:quot;;for ($i=0;$i<strlen($txt);$i++){if ($ctr==strlen($encrypt_key)) $ctr=0;$tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);$ctr++;}return $tmp;}function encrypt($txt,$key){srand((double)microtime()*1000000);$encrypt_key = md5(rand(0,32000));$ctr=0;$tmp = &qu...
  正文:<?PHP
//$key = "This is supposed to be a secret key !!!"; 估计是密阴
$key="这是一个密阴";
function keyED($txt,$encrypt_key)
{
$encrypt_key = md5($encrypt_key);
$ctr=0;
$tmp = "";
for ($i=0;$i<strlen($txt);$i++)
{
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
$ctr++;
}
return $tmp;
}

function encrypt($txt,$key)
{
srand((double)microtime()*1000000);
$encrypt_key = md5(rand(0,32000));
$ctr=0;
$tmp = "";
for ($i=0;$i<strlen($txt);$i++)
{
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($encrypt_key,$ctr,1) .
(substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
$ctr++;
}
return keyED($tmp,$key);
}

function decrypt($txt,$key)
{
$txt = keyED($txt,$key);
$tmp = "";
for ($i=0;$i<strlen($txt);$i++)
{
$md5 = substr($txt,$i,1);
$i++;
$tmp.= (substr($txt,$i,1) ^ $md5);
}
return $tmp;
}

$string = "需要加密的字符串,如密码等.";

// 开始加密 encrypt $string, and store it in $enc_text
$enc_text = encrypt($string,$key);

// 开始解密 decrypt the encrypted text $enc_text, and store it in $dec_text
$dec_text = decrypt($enc_text,$key);

print "原始字符串 : $string <Br>/n";
print "加密后字串 : $enc_text <Br>/n";
print "解密后字串 : $dec_text <Br>/n";
?>
 

来源:http://www.002pc.com/master/College/Programming/PHP/2008-11-27/4184.html

收藏到:

关于《PHP代码加密解密方法》文章的评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面

随机文章

    SQL Error: select * from ***_ecms_article where classid='86' and checked=1 order by rand() limit 10

顶出来的热门

    SQL Error: select * from ***_ecms_article where classid='86' and checked=1 order by diggtop desc,id desc limit 10
站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td