一段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 $array1 = array('a', 'b', 'c', 'd'); $array2 = array('a', 'c'); $array3 =...

判断php数组是否为索引数组的实现方法

HP没有内置判断是否索引数组的方法,简单实现了一个,用法:复制代码 代码如下:echo is_assoc($array)?'索引数组':'不是索引数组';is_assoc函数如下:复制代...

PHP实现的文件上传类与用法详解

本文实例讲述了PHP实现的文件上传类与用法。分享给大家供大家参考,具体如下: FileUpload.class.php,其中用到了两个常量,可在网站配置文件中定义:define('ROO...

php 快速判断一个数字属于什么范围的实现方法

需求是这样 ... if ( $foo > 0 && $foo < 100 ) $bar = 1; elseif ( $foo > 99 && $foo < 2...

PHP 采集获取指定网址的内容

参考别人想法变成自己的想法,你会发现慢慢下来以后你就拥有了临时解决很多问题的思路与方法。复制代码 代码如下:<?php /* 功能:获取页面内容,存储下来阅读; lost63 */...