日期:2014-05-16 浏览次数:20491 次
$id = 1234;
$key = 'aaa';
for($i=1; $i<100; $i++) {
printf("%-10d %s %s\n", $id, $s = encrypt($id++, $key), decrypt( $s, $key));
}
function encrypt($id, $key) {
$dict = array('1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'-','=','*'
);
$n = rand(0, 15);
srand($n);
$key = current(unpack('L', substr(sha1($key, 1), $n)));
$id ^= $key;
$t = str_split(sprintf('%04b%032b', $n, $id), 6);
foreach($t as $i=>&$v) {
$v = $dict[bindec($v)];
if($i == 0) shuffle($dict);
}
return join($t);
}
function decrypt($s, $key) {
$dict = array('1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'-','=','*'
);
$m = array_search($s{0}, $dict);
$n = $m >> 2;
srand($n);
shuffle($dict);
$dict = array_flip($dict);
foreach(str_split($s) as $i=>$c) {
$r[] = sprintf('%06b', $i==0 ? $m&0x03 : $dict[$c]);
}
$id = bindec(join($r));
$key = current(unpack('L', substr(sha1($key, 1), $n)));
return $id ^ $key;
}
1234 4rHK4B 1234