首先在mysql user表中存一个名叫salt的字段为int(2)类型,范围为11-99.
获取user表中的salt
$salt
那十位$m,个位为$n
$m=$salt / 10;
$n=$salt % 10;
//密码为六位以上字符
<?php
function encode($psw,$m,$n) {
//1 位移
//把$psw字符串分成前后两部分
$len=strlen($psw);
//0的情况
$move=abs($len-$m);
//echo $move;
if ($move==0)
{
$front=substr($psw, -1, 1);//y
$back=substr($psw, 0, -1);//cutebo
}
//1的情况
else {
$move -= 1;
$front=substr($psw,0,$move);
$front.=substr($psw,$move+1);//uteboy
$back=substr($psw,$move,1);//c
}
$step1 = $front.$back;
//echo $step1;
// 2 ANSI码位移
$step2='';
for($i=0;$i<$len;$i++) {
$b=substr($step1,$i,1);
$cb=ord($b);
$cb+=$n;
$b=chr($cb);
$step2.=$b;
}
//return $step2;
// 3 对称交换
$yu=$len % 2;
if ($yu) {
$len--;
$avg=$len / 2;
$middle=substr($step2, $avg, 1);
}
else
$avg=$len / 2;
//cuteboy变成yobetuc
$lstr='';
$rstr='';
for($i=0;$i<$avg;$i++) {
$left=$avg-1-$i;
$right=-1-$i;
$lstr.=substr($step2,$left,1);
$rstr.=substr($step2, $right, 1);
}
if (isset($middle))
return $rstr.$middle.$lstr;
return $rstr.$lstr;
}
function decode($psw,$m,$n) {
$len=strlen($psw);
$yu=$len % 2;
if ($yu) {
$len--;
$avg=$len / 2;
$middle=substr($psw, $avg, 1);
$len++;
}
else
$avg=$len / 2;
$front=substr($psw,0,$avg);
$back=substr($psw, -$avg);
$front=strrev($front);
$back=strrev($back);
if (isset($middle))
$step1=$back.$middle.$front;
else
$step1=$back.$front;
// 2 ANSI码位移
$step2='';
for($i=0;$i<$len;$i++) {
$b=substr($step1,$i,1);
$cb=ord($b);
$cb-=$n;
$b=chr($cb);
$step2.=$b;
}
//最后一步
$move=abs($len-$m);
//echo $move;
if ($move==0)
{
$back=substr($step2, 0, 1);//y
$front=substr($step2, 1);//cutebo
}
//1的情况
else {
$move -= 1;
$front=substr($step2,0,$move);
//echo $back;
$front.=substr($step2,-1,1);//c
$back=substr($step2,$move,-1);//uteboy
}
return $front.$back;
}
//测试...
$str='baby520';
$str=encode($str,6,5);
echo $str,' ';
echo decode($str,6,5);
//will output
//g57:~gf baby520