Home >Backend Development >PHP Tutorial >为什么php rc4加密函数的执行结果是乱码?

为什么php rc4加密函数的执行结果是乱码?

PHPz
PHPzOriginal
2016-06-06 20:29:343183browse

php rc4加密函数的执行结果乱码是因为RC4是二进制加密算法,而密文是无法直接当作文本查看的,可以用base64对它编码即可解决乱码问题。

为什么php rc4加密函数的执行结果是乱码?

为什么php rc4加密函数的执行结果是乱码?

以下是执行结果:

j�� �����WAPߔһ�gaUJ_���טzL�

以下是代码部分:

echo rc4('P7j6q1', '20150820');
 
/*
 * rc4加密算法
 * $pwd  密钥
 * $data 要加密的数据
 */
function rc4($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;
}<

因为RC4是二进制加密算法,所以密文是无法直接当作文本查看的,你可以用base64对它编码

更多相关技术知识,请访问PHP中文网

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn