教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php5数字型字符串简易加解密的实现代码

php5数字型字符串简易加解密的实现代码

发布时间:2015-11-18   编辑:jiaochengji.com
一段可以对数字型字符串进行简单的加密与解密的代码,有需要的朋友,可以参考下。

代码如下:

<?php 
/**
 * 数字型字符串简单加密码与解密
 * site www.jbxue.com
*/
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; 
} 
} 
?>

您可能感兴趣的文章:
php5数字型字符串简易加解密的实现代码
php5 字符串处理函数汇总
php数字类型之字符串类型详解
php MD5函数的简单示例
C#对字符串加密与解密的一例代码
php中的加密方法有哪些?
php字符串操作函数入门篇
PHP中的加密功能
PHP字符串函数与使用分析
php url编码与解码(加密/解密)

[关闭]
~ ~