PHP基于流的RC4加密算法实现

基于流的RC4实现。

$rc4 = new RC4(base_decode('abcdefghijklmnopqrstuv=='));

$response = $rc4->encrypt('123456');
$response2 = $rc4->encrypt('123456');

因为是基于流的,response和response2的值会不一样。
想要两次加密的值一样,需要用相同的密钥实例化RC4,再去加密或解密。

具体实现如下

class RC4
{
    private $_s_encrypt = null;
    private $_s_decrypt = null;
    private $_password = null;
    private $_encrypt_offset_i = 0;
    private $_decrypt_offset_i = 0;
    private $_encrypt_offset_j = 0;
    private $_decrypt_offset_j = 0;

	public function getPassword(){
		return $this->_password;
	}
    public function __construct($password)
    {
        $this->_password = array_values( unpack('C*', $password));
        $this->_s_encrypt = $this->init();
        $this->_s_decrypt = array_slice($this->_s_encrypt, 0, 256);
    }

    private function init()
    {
	    $p = $this->_password;
        $j = 0;
        $len = count($p);
        $k = [];
        $s = [];
        for ($i = 0; $i < 256; $i++) {
            $s[$i] = $i;
            $k[$i] = $p[$i % $len];
        }
        for ($i = 0; $i < 256; $i++) {
            $j = (($j + $s[$i] + $k[$i]) & 0xff);
            $tmp = $s[$i];
            $s[$i] = $s[$j]; //交换s[i]和s[j]
            $s[$j] = $tmp;
        }
        return $s;
    }

    public function encrypt($buffer)
    {
	    $buffer = array_values(unpack('C*', $buffer));
        $i = 0; $j = 0; $t = 0;
        $end_offset = count($buffer);
        for ($k = 0; $k < $end_offset; $k++)
        {
            $this->_encrypt_offset_i = $i = ($this->_encrypt_offset_i + 1) & 0xff;

            $this->_encrypt_offset_j = $j = ($this->_encrypt_offset_j + $this->_s_encrypt[$i]) & 0xff;
            $tmp = $this->_s_encrypt[$i];
            $this->_s_encrypt[$i] = $this->_s_encrypt[$j];
            $this->_s_encrypt[$j] = $tmp;
            $t = ($this->_s_encrypt[$i] + $this->_s_encrypt[$j]) & 0xff;
            $buffer[$k] ^= $this->_s_encrypt[$t];
        }
        array_unshift($buffer, 'C*');

        return call_user_func_array('pack', $buffer);
    }

    public function decrypt($buffer)
    {
	    $buffer = array_values(unpack('C*', $buffer));
        $i = 0; $j = 0; $t = 0;
        $end_offset = count($buffer);
        for ($k = 0; $k < $end_offset; $k++)
        {
            $this->_decrypt_offset_i = $i = ($this->_decrypt_offset_i + 1) & 0xff;

            $this->_decrypt_offset_j = $j = ($this->_decrypt_offset_j + $this->_s_decrypt[$i]) & 0xff;
            $tmp = $this->_s_decrypt[$i];
            $this->_s_decrypt[$i] = $this->_s_decrypt[$j];
            $this->_s_decrypt[$j] = $tmp;
            $t = ($this->_s_decrypt[$i] + $this->_s_decrypt[$j]) & 0xff;
            $buffer[$k] ^= $this->_s_decrypt[$t];
        }
        array_unshift($buffer, 'C*');

        return call_user_func_array('pack', $buffer);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anlige

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值