一段php加密解密的代码

yipeiwu_com5年前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 //下面代码会画出一个很漂亮的叶子 // 定义 PI 一分的角度的值 define("PII", M_PI/180); // 新建图像资源,并定义...

PHP字符串的递增和递减示例介绍

今天看到php手册上有这么一段话: “在处理字符变量的算数运算时,PHP 沿袭了 Perl 的习惯,而非 C 的。例如,在 Perl 中 $a = 'Z'; $a++; 将把 $a 变成...

php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)

微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中。 内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/JSAPI <...

有关phpmailer的详细介绍及使用方法

第一,需要下载PHPMailer文件包phpmailer. http://phpmailer.sourceforge.net/第二,确认你的服务器系统已经支持socket ,通过phpi...

PHP中单例模式的使用场景与使用方法讲解

单例模式按字面来看就是指某一个类只存在一个实例,这一点其实是与静态方法是相同的,只存在一个实例的好处是当我们需要使用某个类时只需要实例化一次,不需要每次都去new,极大的降低了资源的耗费...