实例:用PHP来进行加密与解密

时间:2009-12-30 09:20:49   来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:$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()*1...
  正文:

实例应用:使用PHP来进行加密与解密

<?php
$key = "This is supposed to be a secret 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 = "Hello World !!!";
// 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 "Original text : $string <Br>";
print "Encrypted text : $enc_text <Br>";
print "Decrypted text : $dec_text <Br>";
?>


来源:http://www.002pc.com/master/College/Programming/PHP/2008-11-27/4110.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