一段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代码质量36计

1.不要使用相对路径 常常会看到: require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前...

PHP容易被忽略而出错陷阱 数字与字符串比较

0 与任意非数字(或者说,不可转化为数字的字符)前导的字符串比较(操作符为==), 均返回 true. 原因是, 数字与字符串比较时, 先尝试将字符串转换为数字, 再比较, 一个不能转...

PHP常用技巧汇总

本文为大家分享了多个php常用技巧,供大家参考,具体内容如下 1、PHP文件读取函式 //文件读取函式 function PHP_Read($file_name) { $fd=fo...

使用PHP编写的SVN类

复制代码 代码如下:<?php/** * SVN 外部命令 类 * * @author rubekid * * @todo com...

php基于表单密码验证与HTTP验证用法实例

本文实例讲述了php基于表单密码验证与HTTP验证用法。分享给大家供大家参考。具体分析如下: PHP 的 HTTP 认证机制仅在 PHP 以 Apache 模块方式运行时才有效,因此该功...