一段php加密解密的代码

yipeiwu_com6年前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>n";  
print "Encrypted text : $enc_text <Br>n";  
print "Decrypted text : $dec_text <Br>n";  
?>

相关文章

php生成无限栏目树

栏目数组: $arr=Array( Array('cid' => 2,'cname' => '新闻','pid' => 0), Array('cid' =&...

解析php中call_user_func_array的作用

一、直接调用方法复制代码 代码如下:function test($a, $b) {echo '测试一:'.$a.$b;}//调用test方法,array("asp", 'php')对应相...

用js进行url编码后用php反解以及用php实现js的escape功能函数总结

smarty可以直接对url进行编码, 比如<!--{$var|urlencode}--> 但在smarttemplate里面就好像没有,由于链接是由js提交的,而不是表单提...

php中随机函数mt_rand()与rand()性能对比分析

本文实例对比分析了php中随机函数mt_rand()与rand()性能问题。分享给大家供大家参考。具体分析如下: 在php中mt_rand()和rand()函数都是可以随机生成一个纯数字...

php的4种常用运行方式详解

php的4种常用运行方式:CGI、FastCGI、APACHE2HANDLER、CLI。 1、CGI CGI即通用网关接口(common gatewag interface),它是一...