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

PHP
PHP与MySQL开发中页面出现乱码的一种解决方法
在PHP里得到前天和昨天的日期的代码
PHP4和PHP5性能测试和对比 测试代码与环境
wordpress之wp-settings.php
PHP下几种删除目录的方法总结
discuz 首页四格:最新话题+最新回复+热门话题+精华文章插件
海河写的 Discuz论坛帖子调用js的php代码
利用static实现表格的颜色隔行显示的代码
从一个不错的留言本弄的mysql数据库操作类
从MySQL数据库表中取出随机数据的代码
56.com视频采集接口程序(PHP)
php下实现伪 url 的超简单方法[转]
一些常用的php简单命令代码集锦
用windows下编译过的eAccelerator for PHP 5.1.6实现php加速的使用方法
实现php加速的eAccelerator dll支持文件打包下载
使用 eAccelerator加速PHP代码的方法
pw的一个放后门的方法分析
php在线生成ico文件的代码
一个图形显示IP的PHP程序代码
[PHP]实用函数2

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


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