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

PHP
discuz Passport 通行证 整合笔记
flash+php+mysql打造简单留言本教程
[原创]效率较高的php下读取文本文件的代码
phpmyadmin的安装与使用图文教程
IStream与TStream之间的相互转换
继续收藏一些PHP常用函数
php-5.2下php.ini 中文版配置说明
php单件模式结合命令链模式使用说明
PHPMailer邮件类利用smtp.163.com发送邮件方法
PHP编实现程动态图像的创建代码
php仿ZOL分页类代码
php仿discuz分页效果代码
攻克CakePHP(PHP中的Ruby On Rails框架)图文介绍
PHP ajax 分页类代码
PHP和Java 集成开发详解分析 强强联合
Linux下 php5 MySQL5 Apache2 phpMyAdmin ZendOptimizer安装与配置[图文]
IIS php环境配置PHP5 MySQL5 ZendOptimizer phpmyadmin安装与配置
Pear DB 新手入门指南教程
PHPMailer安装方法及简单实例
Apache+php+mysql在windows下的安装与配置图解(最新版)

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


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