首页 > PHP > php实现RC4加密算法的函数

php实现RC4加密算法的函数

/*
* rc4加密算法
* 解密方法:重新加密一次,便可还原。。
* $pwd 密钥
* $data 要加密的数据
*/
function rc4 ($pwd, $data)//$pwd密钥 $data需加密字符串
{
$key[] =””;
$box[] =””;

$pwd_length = strlen($pwd);
$data_length = strlen($data);

for ($i = 0; $i < 256; $i++)
{
$key[$i] = ord($pwd[$i % $pwd_length]);
$box[$i] = $i;
}

for ($j = $i = 0; $i < 256; $i++)
{
$j = ($j + $box[$i] + $key[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}

for ($a = $j = $i = 0; $i < $data_length; $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;
}

分类:PHP 标签:, , ,
  1. 还没有评论。
  1. No trackbacks yet.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: