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

PHP
用 PHP5 轻松解析 XML
扩展你的 PHP 之入门篇
创造世界上最简单的 PHP 开发模式
特转载一高手总结PHP学习资源和链接.
PHP开发入门教程之面向对象
怎样才能成为PHP高手?学会“懒惰”的编程
详解:——如何将图片储存在数据库里
PHP生成静态页面详解
IIS6.0+PHP5.x+MySQL5.x+Zend3.0x+GD+phpMyAdmin2.8x通用安装实例(已经完成)
常用表单验证类,有了这个,一般的验证就都齐了。
PHP文件下载类
一个PHP日历程序
[转帖]PHP世纪万年历
PHP语法速查表
一些常用的php函数
PHP 开发工具
PHP 5.0 Pear安装方法
弄了个检测传输的参数是否为数字的Function
PHP一些有意思的小区别
一段防盗连的PHP代码

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


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