当前位置: 首页 > 图文教程 > 网络编程 > PHP > 一段php加密解密的代码

PHP
使用无限生命期Session的方法
php+oracle 分页类
用PHP读取IMAP邮件
一段php加密解密的代码
基于qmail的完整WEBMAIL解决方案安装详解
用PHP发电子邮件
PHP网站提速三大“软”招
在线短消息收发的程序,不用数据库
PHP的开合式多级菜单程序
PHP的栏目导航程序
PHP系统流量分析的程序
用php写的serv-u的web申请账号的程序
php中的时间处理
在线竞拍系统的PHP实现框架(二)
用文本作数据处理
PHP 和 MySQL 基础教程(一)
浅谈PHP语法(1)
无数据库的详细域名查询程序PHP版(3)
域名查询代码公布
最省空间的计数器

PHP 中的 一段php加密解密的代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 45 ::
收藏到网摘: n/a

<?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";
?>