博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

龙的心·专栏

phpshao.cublog.cn
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); 

 

//加密   
function str2hex($s)   
{       
    $r = "";   
    $hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");   
    for ($i=0; $i<strlen($s); $i++)   
        $r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);   
    return $r;   
}   
  
//解密   
function hex2str($s)   
{   
    $r = "";   
    for ( $i = 0; $i<strlen($s); $i += 2)   
    {   
        $x1 = ord($s{$i});   
        $x1 = ($x1>=48 && $x1<58) ? $x1-48 : $x1-97+10;   
        $x2 = ord($s{$i+1});   
        $x2 = ($x2>=48 && $x2<58) ? $x2-48 : $x2-97+10;   
        $r .= chr((($x1 << 4) & 0xf0) | ($x2 & 0x0f));   
    }   
    return $r;   
}    
 echo str2hex("山东");
 echo "<br>";
 echo hex2str("c9bdb6ab");
?> 

 

发表于: 2008-02-28,修改于: 2008-02-28 15:44,已浏览684次,有评论0条 推荐 投诉

给我留言
版权所有 ChinaUnix.net 页面生成时间:7.46678