当前位置: 首页 > 图文教程 > 网络编程 > PHP > 实例应用:使用PHP来进行加密与解密

PHP
PHP 手机归属地查询 api
php 自写函数代码 获取关键字 去超链接
检查url链接是否已经有参数的php代码 添加 ? 或 &
PHP生成网页快照 不用COM不用扩展.
一步一步学习PHP(1) php开发环境配置
一步一步学习PHP(2):PHP类型
一步一步学习PHP(3) php 函数
一步一步学习PHP(4) php 函数 补充2
提高PHP编程效率 引入缓存机制提升性能
php 数组的合并、拆分、区别取值函数集
PHP采集相关教程之一 CURL函数库
IP138 IP地址查询小偷实现代码
php 生成静态页面的办法与实现代码详细版
一步一步学习PHP(5) 类和对象
一步一步学习PHP(6) 面向对象
Apache环境下PHP利用HTTP缓存协议原理解析及应用分析
PHP 截取字符串函数整理(支持gb2312和utf-8)
php foreach 使用&(与运算符)引用赋值要注意的问题
PHP IPV6正则表达式验证代码
用PHP ob_start()控制浏览器cache、生成html实现代码

实例应用:使用PHP来进行加密与解密


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 33 ::
收藏到网摘: 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>";
print "Encrypted text : $enc_text <Br>";
print "Decrypted text : $dec_text <Br>";
?>