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

PHP
动易数据转成dedecms的php程序
PHP中文汉字验证码
收藏的一个php小偷的核心程序
ASP和PHP都是可以删除自身的
PHP+Tidy-完美的XHTML纠错+过滤
PHP实现MVC开发得最简单的方法——模型
相对路径转化成绝对路径
如何提高MYSQL数据库的查询统计速度 select 索引应用
php下的转义字符串
PHP 中英文混合排版中处理字符串常用的函数
Linux下ZendOptimizer的安装与配置方法
给apache2.2加上mod_encoding模块後 php5.2.0 处理url出现bug
安装PHP可能遇到的问题“无法载入mysql扩展” 的解决方法
dede全站URL静态化改造[070414更正]
使用Xdebug调试和优化PHP程序之[1]
PHP与SQL注入攻击[一]
PHP与SQL注入攻击[二]
PHP与SQL注入攻击[三]
PHP和XSS跨站攻击的防范
Discuz! 5.0.0论坛程序中加入一段js代码,让会员点击下载附件前自动弹出提示窗口

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


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