一段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 require_once 'sqlTools.class.php';//封装类,可执行dql、dml语句 $info=$_POST['info']...

php防止sql注入简单分析

本文实例分析了php防止sql注入简单方法。分享给大家供大家参考。具体如下: 这里只说一个简单的方法 防止Sql注入的方法有很多,这里要说的其实就是漏洞演练平台Dvwa里的一种方式 直接...

php 广告调用类代码(支持Flash调用)

调用方式如下:其中DebugStr这个函数就是类似一个echo。 复制代码 代码如下: DebugStr('$Adv->getContentById($id); $id为广告编号,...

php发送http请求的常用方法分析

本文实例讲述了php发送http请求的常用方法。分享给大家供大家参考,具体如下: http请求有get,post。 php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]。...

PHP实现的分页类定义与用法示例

PHP实现的分页类定义与用法示例

本文实例讲述了PHP实现的分页类定义与用法。分享给大家供大家参考,具体如下: <?php class Page { private $total; /...