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

PHP
php REMOTE_ADDR之获取访客IP的代码
编写漂亮的代码 - 将后台程序与前端程序分开
懒惰是金 介绍几个php通用的函数
追求程序速度,而不是编程的速度
使用PHP的日期与时间函数技巧
php格式化工具Beautify PHP小小BUG
php实现的简单压缩英文字符串的代码
php5数字型字符串加解密代码
awk简介与学习笔记收集
PHP开发框架总结收藏
Ajax PHP简单入门教程代码
php中文字母数字验证码实现代码
php SQL防注入代码
php xfocus防注入资料
用php+javascript实现二级级联菜单的制作
php adodb连接带密码access数据库实例,测试成功
删除及到期域名的查看(抢域名必备哦)
开源SNS系统-ThinkSNS
php循环输出数据库内容的代码
php读取数据库信息的几种方法

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


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