php实现rc4加密算法代码_PHP教程
程序员文章站
2022-05-09 13:46:02
...
代码
/*
* rc4加密算法
* $pwd 密钥
* $data 要加密的数据
*/
function rc4 ($pwd, $data)//$pwd密钥 $data需加密字符串
{
$key[] ="";
$box[] ="";
$pwd_length = strlen($pwd);
$data_length = strlen($data);
for ($i = 0; $i {
$key[$i] = ord($pwd[$i % $pwd_length]);
$box[$i] = $i;
}
for ($j = $i = 0; $i {
$j = ($j + $box[$i] + $key[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for ($a = $j = $i = 0; $i {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$k = $box[(($box[$a] + $box[$j]) % 256)];
$cipher .= chr(ord($data[$i]) ^ $k);
}
return $cipher;
}
复制代码 代码如下:
/*
* rc4加密算法
* $pwd 密钥
* $data 要加密的数据
*/
function rc4 ($pwd, $data)//$pwd密钥 $data需加密字符串
{
$key[] ="";
$box[] ="";
$pwd_length = strlen($pwd);
$data_length = strlen($data);
for ($i = 0; $i {
$key[$i] = ord($pwd[$i % $pwd_length]);
$box[$i] = $i;
}
for ($j = $i = 0; $i {
$j = ($j + $box[$i] + $key[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for ($a = $j = $i = 0; $i {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$k = $box[(($box[$a] + $box[$j]) % 256)];
$cipher .= chr(ord($data[$i]) ^ $k);
}
return $cipher;
}