当前位置: 首页 > 图文教程 > 网络编程 > PHP > php5数字型字符串加解密代码

PHP
EPSON打印机 连供墨水系统 维修有哪些保养窍门
最简便的MySql数据库备份方法MySQLdump
台湾中原大学php教程孙仲岳主讲
讲的非常不错的PHP编码规范
PHP读取目录下所有文件的代码
使用ETags减少Web应用带宽和负载
Http 1.1 Etag 与 Last-Modified提高php效率
优化使用mysql存储session的php代码
php下MYSQL limit的优化
深入解析php模板技术原理【一】
php框架Phpbean说明
Phpbean路由转发的php代码
php模板之Phpbean的目录结构
set_include_path在win和linux下的区别
php扩展ZF——Validate扩展
在PHP中使用Sockets 从Usenet中获取文件
逐步提升php框架的性能
ZF等常用php框架中存在的问题
php下使用SMTP发邮件的代码
让PHP支持页面回退的两种方法

PHP 中的 php5数字型字符串加解密代码


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

对应awk版加解密程序的PHP实现代码
<?php
/* ----------------------------------------------------------------------------
* Script Name: encrypt.php
* Creation Date: 2008-4-7 10:36
* Last Modified: 2008-4-12 16:00
* Author: meyu
* Copyright (c) 2007
* Purpose: 数字字符串简易加解密
* ----------------------------------------------------------------------------*/
class Encryption {
/**
* 最终的密文代码,可设为任意不重复的10位英文字符a-zA-Z
*/
private $replacement = 'urskydMeIV';
/**
* 增加的密文第一位,可设为1位除0以外的整数,即 1-9
*/
private $prefix = "8";
/**
* 公钥,长度小于8位的正整数
*/
private $match = "111111";
/**
* 转换后对照数组
*/
private $replaceenc;
private $replacedec;
function __construct() {
for($i =0; $i < 10; $i++) {
$this->replaceenc['/'.$i.'/'] = $this->replacement{$i};
$this->replacedec['/'.$this->replacement{$i}.'/'] = $i;
}
}
public function encrypt($str) {
return preg_replace(
array_keys($this->replaceenc),
$this->replaceenc,
$this->mynotin(preg_replace("/(.)(.)/", "${2}${1}", $str))
);
}
public function decrypt($str) {
return preg_replace("/(.)(.)/", "${2}${1}",
$this->mynotout(preg_replace(array_keys($this->replacedec),$this->replacedec,$str))
);
}
private function mynotin($str) {
$str_out = "";
$i = 0;
while(isset($str{7*$i})) {
$str_out .= (($this->prefix.substr($str, $i*7, 7))+0)^$this->match;
$i++;
}
return $str_out;
}
private function mynotout($str) {
$str_out = "";
$i = 0;
while(isset($str{8*$i})) {
$str_out .= substr((substr($str, $i*8, 8)+0)^$this->match, 1);
$i++;
}
return $str_out;
}
}
?>