一段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 是一种HTML 内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,现在被很多的网站编程人员广泛...

PHP7 新特性详细介绍

PHP 的学习新特性 最近做的项目使用了 php7,但感觉有很多新特性没有用起来。就想总结一下,一些可能会用到的新特性。之前使用的环境是 php5.4,所有也会有 php5.5 和 ph...

phpinfo的知识点总结

phpinfo是一个运行指令,为显示php服务器的配置信息。 phpinfo-输出大量PHP信息 bool phpinfo() 输出 PHP 当前状态的大量信息,包含了 PHP 编...

crontab无法执行php的解决方法

本文分析了crontab无法执行php的解决方法。分享给大家供大家参考,具体如下: 用crontab跑php程序时,如何去调试,各人有各人的方法。我也有套方法,看一下,我是如何解决cro...

php中几种常见安全设置详解

另外,目前闹的轰轰烈烈的SQL Injection也是在PHP上有很多利用方式,所以要保证安全,PHP代码编写是一方面,PHP的配置更是非常关键。 我们php手手工安装的,php的默认...