RC4 encryption is garbled after the problem
This is what I found on the internet. RC4 Encryption Example:
function Rc4_zz ($data, $pwd = "")//$pwd key $data need to encrypt string
{
$key [] = "";
$box [] = "";
$cipher = "";
$pwd _length = strlen ($PWD);
$data _length = strlen ($data);
for ($i = 0; $i < $i + +) {
$key [$i] = Ord ($pwd [$i% $pwd _length]);
$box [$i] = $i;
}
for ($j = $i = 0; $i < $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;
}
After executing the function, the result is garbled:?????? Similar to this
I directly in the running platform directly output any character is normal, is the use of his encryption function, is garbled. Not in the database (thought to be forced by other means, but the result of execution, decryption is not the source data!) )
Who used to know why teach a bit, thank you, online and so on!
(Do not recommend me with MD5 Oh, this mission requires to be able to completely restore the public and private encryption!) )
------Solution--------------------
According to the RC4 algorithm, your code should be written
$pwd key $data need to encrypt string
function Rc4_zz ($data, $pwd = "") {
$key [] = "";
$box [] = "";
$cipher = "";
$pwd = Str_pad ($pwd, N. chr (0)); Add this sentence.
$pwd _length = strlen ($PWD);
$data _length = strlen ($data);
for ($i = 0; $i < $i + +) {
$key [$i] = Ord ($pwd [$i% $pwd _length]);
$box [$i] = $i;
}
for ($j = $i = 0; $i < $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;
}