上个礼拜和一个同事一起做个项目,我有一个字符串要传给他,考虑到安全问题,决定混淆一下再传,所以临时写了这个混淆字符串的类。同事原本用C#写好了个类似的,我在原有基础上改写成了PHP的 ^^
下载地址 http://phpjoe.512j.com/disarrange_class.rar
在此要感谢 Arron Wong ^^
<?
//////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Programmed by Verne Joe on 2005-01-15.
// Thankfulness to Arron Wong. He told me the arithmetic ^^
//
//////////////////////////////////////////////////////////////////////////////////////////////////////
class disarrange{
var $arr;
//constructed function and turn string into array
function disarrange($str){
$string = preg_replace("/(.{1})/", "\1,",$str);
$this->arr = explode(",", substr($string, 0,-1));
}
//function for encrypting
function encrypt(){
$arr = $this->MakeIntArray($this->arr);
for($i=0;$i<count($this->arr);$i++){
$ch = $this->arr[$i];
$this->arr[$i] = $this->arr[$arr[$i]];
$this->arr[$arr[$i]] = $ch;
}
return $this->revertToString($this->arr);
}
//function for decrypting
function decrypt(){
$arr = $this->MakeIntArray($this->arr);
for($i=count($this->arr)-1;$i>=0;$i--){
$ch = $this->arr[$i];
$this->arr[$i] = $this->arr[$arr[$i]];
$this->arr[$arr[$i]] = $ch;
}
return $this->revertToString($this->arr);
}
function MakeIntArray($arr){
$confuse = new PseudoRandomizer(count($arr) * count($arr), count($arr));
for($i=0;$i<count($arr);$i++){
$arr[$i] = $confuse->Generate();
}
return $arr;
}
//revert array to string
function revertToString($arr){
$string = "";
foreach ($arr as $str)
$string.= $str;
return $string;
}
}
class PseudoRandomizer{
var $seed;
var $max;
function PseudoRandomizer($seed,$max){
$this->seed = $seed;
$this->max = $max;
}
function Generate(){
$newseed = ($this->seed * (3719/2)) + 3719;
$newseed = $newseed % 65535;
$this->seed = $newseed;
return $this->seed % $this->max;
}
}
?>
?