123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?php
- if (!class_exists('Crypt_Base')) {
- include_once 'Base.php';
- }
- define('CRYPT_RC4_ENCRYPT', 0);
- define('CRYPT_RC4_DECRYPT', 1);
- class Crypt_RC4 extends Crypt_Base
- {
-
- var $block_size = 0;
-
- var $key_length = 128;
-
- var $const_namespace = 'RC4';
-
- var $cipher_name_mcrypt = 'arcfour';
-
- var $use_inline_crypt = false;
-
- var $key;
-
- var $stream;
-
- function __construct()
- {
- parent::__construct(CRYPT_MODE_STREAM);
- }
-
- function Crypt_RC4()
- {
- $this->__construct();
- }
-
- function isValidEngine($engine)
- {
- if ($engine == CRYPT_ENGINE_OPENSSL) {
- if (version_compare(PHP_VERSION, '5.3.7') >= 0) {
- $this->cipher_name_openssl = 'rc4-40';
- } else {
- switch (strlen($this->key)) {
- case 5:
- $this->cipher_name_openssl = 'rc4-40';
- break;
- case 8:
- $this->cipher_name_openssl = 'rc4-64';
- break;
- case 16:
- $this->cipher_name_openssl = 'rc4';
- break;
- default:
- return false;
- }
- }
- }
- return parent::isValidEngine($engine);
- }
-
- function setIV($iv)
- {
- }
-
- function setKeyLength($length)
- {
- if ($length < 8) {
- $this->key_length = 1;
- } elseif ($length > 2048) {
- $this->key_length = 256;
- } else {
- $this->key_length = $length >> 3;
- }
- parent::setKeyLength($length);
- }
-
- function encrypt($plaintext)
- {
- if ($this->engine != CRYPT_ENGINE_INTERNAL) {
- return parent::encrypt($plaintext);
- }
- return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
- }
-
- function decrypt($ciphertext)
- {
- if ($this->engine != CRYPT_ENGINE_INTERNAL) {
- return parent::decrypt($ciphertext);
- }
- return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
- }
-
- function _setupKey()
- {
- $key = $this->key;
- $keyLength = strlen($key);
- $keyStream = range(0, 255);
- $j = 0;
- for ($i = 0; $i < 256; $i++) {
- $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
- $temp = $keyStream[$i];
- $keyStream[$i] = $keyStream[$j];
- $keyStream[$j] = $temp;
- }
- $this->stream = array();
- $this->stream[CRYPT_RC4_DECRYPT] = $this->stream[CRYPT_RC4_ENCRYPT] = array(
- 0,
- 0,
- $keyStream
- );
- }
-
- function _crypt($text, $mode)
- {
- if ($this->changed) {
- $this->_setup();
- $this->changed = false;
- }
- $stream = &$this->stream[$mode];
- if ($this->continuousBuffer) {
- $i = &$stream[0];
- $j = &$stream[1];
- $keyStream = &$stream[2];
- } else {
- $i = $stream[0];
- $j = $stream[1];
- $keyStream = $stream[2];
- }
- $len = strlen($text);
- for ($k = 0; $k < $len; ++$k) {
- $i = ($i + 1) & 255;
- $ksi = $keyStream[$i];
- $j = ($j + $ksi) & 255;
- $ksj = $keyStream[$j];
- $keyStream[$i] = $ksj;
- $keyStream[$j] = $ksi;
- $text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]);
- }
- return $text;
- }
- }
|