/

0 加密 1 解密

@param $instr

rc4phpPHP RC4算法 Python

@param $operation

@param $expiry

@param $key

@return String

/

function _tool_rc4($instr, $operation = 1, $expiry = 0 ,$key = \公众\"大众)

{

$instr = str_replace(array(\"大众_\公众,\"大众-\"大众),array(\公众+\公众,\"大众/\"大众),$instr);

$ckey_length = 4;

$key = md5($key ? $key : '');

$keya = md5(substr($key, 0, 16));

$keyb = md5(substr($key, 16, 16));

$keyc = $ckey_length ? ($operation > 0 ? substr($instr, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

$cryptkey = $keya.md5($keya.$keyc);

$key_length = strlen($cryptkey);

$instr = $operation > 0 ? base64_decode(substr($instr, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($instr.$keyb), 0, 16).$instr;

$string_length = strlen($instr);

$result = '';

$box = range(0, 255);

$rndkey = array();

for($i = 0; $i <= 255; $i++) $rndkey[$i] = ord($cryptkey[$i % $key_length]);

for($j = $i = 0; $i < 256; $i++) {

$j = ($j + $box[$i] + $rndkey[$i]) % 256;

$tmp = $box[$i];

$box[$i] = $box[$j];

$box[$j] = $tmp;

}

unset($rndkey);

for($a = $j = $i = 0; $i < $string_length; $i++) {

$a = ($a + 1) % 256;

$j = ($j + $box[$a]) % 256;

$tmp = $box[$a];

$box[$a] = $box[$j];

$box[$j] = $tmp;

$result .= chr(ord($instr[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));

}

if($operation > 0) {

if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) return substr($result, 26);

return '';

}

return $keyc.str_replace(array(\公众=\"大众,\公众+\"大众,\"大众/\公众),array(\"大众\公众,\公众_\"大众,\"大众-\"大众), base64_encode($result));

}

//加密

$key = \公众┬┭┮\"大众;

echo _tool_rc4(\"大众小明:1344\"大众, 0, 0 ,$key);

//输出 445atmId_Nq-Og3g00CRw4s9LKcZKmIPqCfD-FYVGIug7H2IMDo

//解密

echo _tool_rc4(\"大众445atmId_Nq-Og3g00CRw4s9LKcZKmIPqCfD-FYVGIug7H2IMDo\公众, 1, 0 ,$key);

//输出 小明:1344

PS:补图,上面的方便复制