RC4 PHP解密失败

RC4 PHP解密失败,php,encryption,rc4-cipher,Php,Encryption,Rc4 Cipher,我使用rc4加密对字符串进行了加密。我可以解密我加密的第一个字符串。但我无法成功解密第二个字符串。有什么想法吗 我的代码 $dpl = 256; $file = fopen( 'log', "w"); $key = 'sjhdjhd'; $content1 = rc4($key,str_repeat(" ",$dpl).'ABC'); $content1 = substr($content1,$dpl); $content2 = rc4($key,str_repeat(" ",$dpl).'DE

我使用rc4加密对字符串进行了加密。我可以解密我加密的第一个字符串。但我无法成功解密第二个字符串。有什么想法吗

我的代码

$dpl = 256;
$file = fopen( 'log', "w");
$key = 'sjhdjhd';
$content1 = rc4($key,str_repeat(" ",$dpl).'ABC');
$content1 = substr($content1,$dpl);
$content2 = rc4($key,str_repeat(" ",$dpl).'DEFG');
$content2 = substr($content2,$dpl);
fwrite($file, $content1);
fwrite($file, $content2);
fclose( $file );

$file = fopen( 'log', "r");
while ( $buff = fread( $file, 256 ) ) {
    $plaintext = rc4($key,str_repeat(" ",$dpl).$buff);
    echo substr($plaintext,$dpl).PHP_EOL;
}

fclose( $file );


function rc4($key, $data)
{
    // Store the vectors "S" has calculated
    static $SC;
    // Function to swaps values of the vector "S"
    $swap = create_function('&$v1, &$v2', '
        $v1 = $v1 ^ $v2;
        $v2 = $v1 ^ $v2;
        $v1 = $v1 ^ $v2;
    ');
    $ikey = crc32($key);
    if (!isset($SC[$ikey])) {
        // Make the vector "S", basead in the key
        $S    = range(0, 255);
        $j    = 0;
        $n    = strlen($key);
        for ($i = 0; $i < 255; $i++) {
            $char  = ord($key{$i % $n});
            $j     = ($j + $S[$i] + $char) % 256;
            $swap($S[$i], $S[$j]);
        }
        $SC[$ikey] = $S;
    } else {
        $S = $SC[$ikey];
    }
    // Crypt/decrypt the data
    $n    = strlen($data);
    $data = str_split($data, 1);
    $i    = $j = 0;
    for ($m = 0; $m < $n; $m++) {
        $i        = ($i + 1) % 256;
        $j        = ($j + $S[$i]) % 256;
        $swap($S[$i], $S[$j]);
        $char     = ord($data[$m]);
        $char     = $S[($S[$i] + $S[$j]) % 256] ^ $char;
        $data[$m] = chr($char);
    }
    return implode('', $data);
}
$dpl=256;
$file=fopen('log','w');
$key='sjhdjhd';
$content1=rc4($key,str_repeat(“,$dpl)。'ABC');
$content1=substr($content1,$dpl);
$content2=rc4($key,str_repeat(“,$dpl)。'DEFG');
$content2=substr($content2,$dpl);
fwrite($file,$content1);
fwrite($file,$content2);
fclose($文件);
$file=fopen('log','r');
而($buff=fread($file,256)){
$plaintext=rc4($key,str_repeat(“,$dpl)。$buff);
echo substr($plaintext,$dpl).PHP\u EOL;
}
fclose($文件);
功能rc4($key$data)
{
//存储已计算的向量“S”
静态$SC;
//函数交换向量“S”的值
$swap=create_函数('&$v1,&$v2','
$v1=$v1^$v2;
$v2=$v1^$v2;
$v1=$v1^$v2;
');
$ikey=crc32($key);
如果(!isset($SC[$ikey])){
//将矢量设为“S”,键中的基为AD
$S=范围(0255);
$j=0;
$n=strlen($key);
对于($i=0;$i<255;$i++){
$char=ord($key{$i%$n});
$j=($j+$S[$i]+$char)%256;
美元掉期($S[$i],$S[$j]);
}
$SC[$ikey]=$S;
}否则{
$S=$SC[$ikey];
}
//加密/解密数据
$n=strlen($data);
$data=str_split($data,1);
$i=$j=0;
对于($m=0;$m<$n;$m++){
$i=($i+1)%256;
$j=($j+$S[$i])%256;
美元掉期($S[$i],$S[$j]);
$char=ord($data[$m]);
$char=$S[($S[$i]+$S[$j])%256]^$char;
$data[$m]=chr($char);
}
返回内爆(“”,$data);
}

您确定不想在开始解密之前重置S向量的状态吗?我已将S向量设为正态变量。结果仍然相同。