PHP implements rc4 encryption algorithm

WBOY
Release: 2016-07-25 09:09:32
Original
2886 people have browsed it

The decryption method of this algorithm is to re-encrypt once and then it can be restored. .

  1. /*
  2. * rc4 encryption algorithm
  3. * $pwd key
  4. * $data data to be encrypted
  5. */
  6. function rc4 ($pwd, $data)//$pwd key $data needs to be encrypted string
  7. {
  8. $key[] ="";
  9. $box[] ="";
  10. $pwd_length = strlen($pwd);
  11. $data_length = strlen($data);
  12. for ($i = 0; $i < 256; $i++)
  13. {
  14. $key[$i] = ord($pwd[$i % $pwd_length]);
  15. $box[$i] = $i;
  16. }
  17. for ($ j = $i = 0; $i < 256; $i++)
  18. {
  19. $j = ($j + $box[$i] + $key[$i]) % 256;
  20. $tmp = $box[ $i];
  21. $box[$i] = $box[$j];
  22. $box[$j] = $tmp;
  23. }
  24. for ($a = $j = $i = 0; $i < ; $data_length; $i++)
  25. {
  26. $a = ($a + 1) % 256;
  27. $j = ($j + $box[$a]) % 256;
  28. $tmp = $box[$a ];
  29. $box[$a] = $box[$j];
  30. $box[$j] = $tmp;
  31. $k = $box[(($box[$a] + $box[$j ]) % 256)];
  32. $cipher .= chr(ord($data[$i]) ^ $k);
  33. }
  34. return $cipher;
  35. }
Copy code


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template