From 376b2759c9d83615e0f77d9a119e96413c1e5d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans-J=C3=BCrgen=20Petrich?= Date: Mon, 21 Jan 2013 21:01:37 +0700 Subject: [PATCH] RC4: fixed ContinuousBuffer() using MODE_MCRYPT Fixed multiple calls to ContinuousBuffer() using MODE_MCRYPT --- phpseclib/Crypt/RC4.php | 49 +++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/phpseclib/Crypt/RC4.php b/phpseclib/Crypt/RC4.php index 48454ef2..14bf67e4 100644 --- a/phpseclib/Crypt/RC4.php +++ b/phpseclib/Crypt/RC4.php @@ -192,6 +192,9 @@ class Crypt_RC4 { case defined('MCRYPT_RC4'); $this->mode = MCRYPT_RC4; } + $this->encryptStream = mcrypt_module_open($this->mode, '', MCRYPT_MODE_STREAM, ''); + $this->decryptStream = mcrypt_module_open($this->mode, '', MCRYPT_MODE_STREAM, ''); + } } @@ -349,18 +352,11 @@ class Crypt_RC4 { if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) { $keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream'; - if ($this->$keyStream === false) { - $this->$keyStream = mcrypt_module_open($this->mode, '', MCRYPT_MODE_STREAM, ''); - mcrypt_generic_init($this->$keyStream, $this->key, ''); - } else if (!$this->continuousBuffer) { - mcrypt_generic_init($this->$keyStream, $this->key, ''); + if ($this->continuousBuffer) { + return mcrypt_generic($this->$keyStream, $text); } - $newText = mcrypt_generic($this->$keyStream, $text); - if (!$this->continuousBuffer) { - mcrypt_generic_deinit($this->$keyStream); - } - - return $newText; + mcrypt_generic_init($this->$keyStream, $this->key, ''); + return mcrypt_generic($this->$keyStream, $text); } if ($this->encryptStream === false) { @@ -442,6 +438,11 @@ class Crypt_RC4 { */ function enableContinuousBuffer() { + if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) { + mcrypt_generic_init($this->encryptStream, $this->key, ''); + mcrypt_generic_init($this->decryptStream, $this->key, ''); + } + $this->continuousBuffer = true; } @@ -457,7 +458,7 @@ class Crypt_RC4 { { if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_INTERNAL ) { $this->encryptIndex = $this->decryptIndex = array(0, 0); - $this->setKey($this->key); + $this->encryptStream = $this->decryptStream = false; } $this->continuousBuffer = false; @@ -508,24 +509,10 @@ class Crypt_RC4 { */ function _closeMCrypt() { - if ( $this->encryptStream !== false ) { - if ( $this->continuousBuffer ) { - mcrypt_generic_deinit($this->encryptStream); - } - - mcrypt_module_close($this->encryptStream); - - $this->encryptStream = false; - } - - if ( $this->decryptStream !== false ) { - if ( $this->continuousBuffer ) { - mcrypt_generic_deinit($this->decryptStream); - } - - mcrypt_module_close($this->decryptStream); - - $this->decryptStream = false; - } + mcrypt_module_close($this->encryptStream); + mcrypt_module_close($this->decryptStream); } } + +// vim: ts=4:sw=4:et: +// vim6: fdl=1: