Rc4.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Dave Mertens <dmertens@zyprexia.com> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Rc4.php,v 1.6 2003/10/04 16:39:32 zyprexia Exp $
  20. /**
  21. * RC4 stream cipher routines implementation
  22. *
  23. * in PHP4 based on code written by Damien Miller <djm@mindrot.org>
  24. *
  25. * Usage:
  26. * $key = "pear";
  27. * $message = "PEAR rulez!";
  28. *
  29. * $rc4 = new Crypt_RC4;
  30. * $rc4->key($key);
  31. * echo "Original message: $message <br>\n";
  32. * $rc4->crypt($message);
  33. * echo "Encrypted message: $message <br>\n";
  34. * $rc4->decrypt($message);
  35. * echo "Decrypted message: $message <br>\n";
  36. *
  37. * @version $Revision: 1.6 $
  38. * @access public
  39. * @package Crypt
  40. * @author Dave Mertens <dmertens@zyprexia.com>
  41. */
  42. class Crypt_RC4 {
  43. /**
  44. * Real programmers...
  45. * @var array
  46. */
  47. var $s= array();
  48. /**
  49. * Real programmers...
  50. * @var array
  51. */
  52. var $i= 0;
  53. /**
  54. * Real programmers...
  55. * @var array
  56. */
  57. var $j= 0;
  58. /**
  59. * Key holder
  60. * @var string
  61. */
  62. var $_key;
  63. /**
  64. * Constructor
  65. * Pass encryption key to key()
  66. *
  67. * @see key()
  68. * @param string key - Key which will be used for encryption
  69. * @return void
  70. * @access public
  71. */
  72. function Crypt_RC4($key = null) {
  73. if ($key != null) {
  74. $this->setKey($key);
  75. }
  76. }
  77. function setKey($key) {
  78. if (strlen($key) > 0)
  79. $this->_key = $key;
  80. }
  81. /**
  82. * Assign encryption key to class
  83. *
  84. * @param string key - Key which will be used for encryption
  85. * @return void
  86. * @access public
  87. */
  88. function key(&$key) {
  89. $len= strlen($key);
  90. for ($this->i = 0; $this->i < 256; $this->i++) {
  91. $this->s[$this->i] = $this->i;
  92. }
  93. $this->j = 0;
  94. for ($this->i = 0; $this->i < 256; $this->i++) {
  95. $this->j = ($this->j + $this->s[$this->i] + ord($key[$this->i % $len])) % 256;
  96. $t = $this->s[$this->i];
  97. $this->s[$this->i] = $this->s[$this->j];
  98. $this->s[$this->j] = $t;
  99. }
  100. $this->i = $this->j = 0;
  101. }
  102. /**
  103. * Encrypt function
  104. *
  105. * @param string paramstr - string that will encrypted
  106. * @return void
  107. * @access public
  108. */
  109. function crypt(&$paramstr) {
  110. //Init key for every call, Bugfix 22316
  111. $this->key($this->_key);
  112. $len= strlen($paramstr);
  113. for ($c= 0; $c < $len; $c++) {
  114. $this->i = ($this->i + 1) % 256;
  115. $this->j = ($this->j + $this->s[$this->i]) % 256;
  116. $t = $this->s[$this->i];
  117. $this->s[$this->i] = $this->s[$this->j];
  118. $this->s[$this->j] = $t;
  119. $t = ($this->s[$this->i] + $this->s[$this->j]) % 256;
  120. $paramstr[$c] = chr(ord($paramstr[$c]) ^ $this->s[$t]);
  121. }
  122. }
  123. /**
  124. * Decrypt function
  125. *
  126. * @param string paramstr - string that will decrypted
  127. * @return void
  128. * @access public
  129. */
  130. function decrypt(&$paramstr) {
  131. //Decrypt is exactly the same as encrypting the string. Reuse (en)crypt code
  132. $this->crypt($paramstr);
  133. }
  134. } //end of RC4 class
  135. ?>