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

PHP
PHP 和 MySQL 基础教程(二)
随机广告显示(PHP函数)
PHP 和 MySQL 基础教程(三)
无数据库的详细域名查询程序PHP版(4)
无数据库的详细域名查询程序PHP版(5)
PHP 和 MySQL 基础教程(四)
谈谈PHP语法(3)
谈谈PHP语法(4)
谈谈PHP语法(5)
文章推荐系统(二)
资料注册后发信小技巧
推荐文章系统(一)
多重條件組合查詢(一)
文章推荐系统(三)
多重條件組合查詢(二)
3种平台下安装php4经验点滴
PHP与MySQL交互使用详解
多数据表共用一个页的新闻发布
层叠菜单的动态生成
模拟OICQ的实现思路和核心程序(二)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 48 ::
收藏到网摘: 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";
?>