一段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上传图片类及用法示例

本文实例讲述了php上传图片类及用法。分享给大家供大家参考,具体如下: 1.类文件名为:upclass.php <?php class upclass{ public...

获取php页面执行时间,数据库读写次数,函数调用次数等(THINKphp)

获取php页面执行时间,数据库读写次数,函数调用次数等(THINKphp)

THINKphp里面有调试运行状态的效果: Process:0.2463s (Load:0.0003s Init:0.0010s Exec:0.1095s Template:0.1355...

php实现的二分查找算法示例

本文实例讲述了php实现的二分查找算法。分享给大家供大家参考,具体如下: <?php $arr = array(4,58,11,34,88,45,32,54,63,78...

php中通过Ajax如何实现异步文件上传的代码实例

1:取得file对象 2:读取2进制数据 3:模拟http请求,把数据发送出去(这里通常比较麻烦) 在forefox下使用 xmlhttprequest 对象的 sendasbinary...

如何写php守护进程(Daemon)

守护进程(Daemon)是运行在后台的一种特殊进程。它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件。守护进程是一种很有用的进程。php也可以实现守护进程的功能。 一、基...