Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 58 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Crypt_RC4 | |
0.00% |
0 / 58 |
|
0.00% |
0 / 8 |
380 | |
0.00% |
0 / 1 |
| Crypt_RC4 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isValidEngine | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| setIV | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| encrypt | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| decrypt | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| _setupKey | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| _crypt | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Pure-PHP implementation of RC4. |
| 5 | * |
| 6 | * Uses mcrypt, if available, and an internal implementation, otherwise. |
| 7 | * |
| 8 | * PHP versions 4 and 5 |
| 9 | * |
| 10 | * Useful resources are as follows: |
| 11 | * |
| 12 | * - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm} |
| 13 | * - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4} |
| 14 | * |
| 15 | * RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not |
| 16 | * ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification. |
| 17 | * |
| 18 | * Here's a short example of how to use this library: |
| 19 | * <code> |
| 20 | * <?php |
| 21 | * include 'Crypt/RC4.php'; |
| 22 | * |
| 23 | * $rc4 = new Crypt_RC4(); |
| 24 | * |
| 25 | * $rc4->setKey('abcdefgh'); |
| 26 | * |
| 27 | * $size = 10 * 1024; |
| 28 | * $plaintext = ''; |
| 29 | * for ($i = 0; $i < $size; $i++) { |
| 30 | * $plaintext.= 'a'; |
| 31 | * } |
| 32 | * |
| 33 | * echo $rc4->decrypt($rc4->encrypt($plaintext)); |
| 34 | * ?> |
| 35 | * </code> |
| 36 | * |
| 37 | * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy |
| 38 | * of this software and associated documentation files (the "Software"), to deal |
| 39 | * in the Software without restriction, including without limitation the rights |
| 40 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 41 | * copies of the Software, and to permit persons to whom the Software is |
| 42 | * furnished to do so, subject to the following conditions: |
| 43 | * |
| 44 | * The above copyright notice and this permission notice shall be included in |
| 45 | * all copies or substantial portions of the Software. |
| 46 | * |
| 47 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 48 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 49 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 50 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 51 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 52 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 53 | * THE SOFTWARE. |
| 54 | * |
| 55 | * @category Crypt |
| 56 | * @package Crypt_RC4 |
| 57 | * @author Jim Wigginton <terrafrost@php.net> |
| 58 | * @copyright 2007 Jim Wigginton |
| 59 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 60 | * @link http://phpseclib.sourceforge.net |
| 61 | */ |
| 62 | |
| 63 | /** |
| 64 | * Include Crypt_Base |
| 65 | * |
| 66 | * Base cipher class |
| 67 | */ |
| 68 | if (!class_exists('Crypt_Base')) { |
| 69 | include_once 'Base.php'; |
| 70 | } |
| 71 | |
| 72 | /**#@+ |
| 73 | * @access private |
| 74 | * @see Crypt_RC4::_crypt() |
| 75 | */ |
| 76 | define('CRYPT_RC4_ENCRYPT', 0); |
| 77 | define('CRYPT_RC4_DECRYPT', 1); |
| 78 | /**#@-*/ |
| 79 | |
| 80 | /** |
| 81 | * Pure-PHP implementation of RC4. |
| 82 | * |
| 83 | * @package Crypt_RC4 |
| 84 | * @author Jim Wigginton <terrafrost@php.net> |
| 85 | * @access public |
| 86 | */ |
| 87 | class Crypt_RC4 extends Crypt_Base |
| 88 | { |
| 89 | /** |
| 90 | * Block Length of the cipher |
| 91 | * |
| 92 | * RC4 is a stream cipher |
| 93 | * so we the block_size to 0 |
| 94 | * |
| 95 | * @see Crypt_Base::block_size |
| 96 | * @var Integer |
| 97 | * @access private |
| 98 | */ |
| 99 | var $block_size = 0; |
| 100 | |
| 101 | /** |
| 102 | * The default password key_size used by setPassword() |
| 103 | * |
| 104 | * @see Crypt_Base::password_key_size |
| 105 | * @see Crypt_Base::setPassword() |
| 106 | * @var Integer |
| 107 | * @access private |
| 108 | */ |
| 109 | var $password_key_size = 128; // = 1024 bits |
| 110 | |
| 111 | /** |
| 112 | * The namespace used by the cipher for its constants. |
| 113 | * |
| 114 | * @see Crypt_Base::const_namespace |
| 115 | * @var String |
| 116 | * @access private |
| 117 | */ |
| 118 | var $const_namespace = 'RC4'; |
| 119 | |
| 120 | /** |
| 121 | * The mcrypt specific name of the cipher |
| 122 | * |
| 123 | * @see Crypt_Base::cipher_name_mcrypt |
| 124 | * @var String |
| 125 | * @access private |
| 126 | */ |
| 127 | var $cipher_name_mcrypt = 'arcfour'; |
| 128 | |
| 129 | /** |
| 130 | * Holds whether performance-optimized $inline_crypt() can/should be used. |
| 131 | * |
| 132 | * @see Crypt_Base::inline_crypt |
| 133 | * @var mixed |
| 134 | * @access private |
| 135 | */ |
| 136 | var $use_inline_crypt = false; // currently not available |
| 137 | |
| 138 | /** |
| 139 | * The Key |
| 140 | * |
| 141 | * @see Crypt_RC4::setKey() |
| 142 | * @var String |
| 143 | * @access private |
| 144 | */ |
| 145 | var $key = "\0"; |
| 146 | |
| 147 | /** |
| 148 | * The Key Stream for decryption and encryption |
| 149 | * |
| 150 | * @see Crypt_RC4::setKey() |
| 151 | * @var Array |
| 152 | * @access private |
| 153 | */ |
| 154 | var $stream; |
| 155 | |
| 156 | /** |
| 157 | * Default Constructor. |
| 158 | * |
| 159 | * Determines whether or not the mcrypt extension should be used. |
| 160 | * |
| 161 | * @see Crypt_Base::Crypt_Base() |
| 162 | * @return Crypt_RC4 |
| 163 | * @access public |
| 164 | */ |
| 165 | function Crypt_RC4() |
| 166 | { |
| 167 | parent::Crypt_Base(CRYPT_MODE_STREAM); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Test for engine validity |
| 172 | * |
| 173 | * This is mainly just a wrapper to set things up for Crypt_Base::isValidEngine() |
| 174 | * |
| 175 | * @see Crypt_Base::Crypt_Base() |
| 176 | * @param Integer $engine |
| 177 | * @access public |
| 178 | * @return Boolean |
| 179 | */ |
| 180 | function isValidEngine($engine) |
| 181 | { |
| 182 | switch ($engine) { |
| 183 | case CRYPT_ENGINE_OPENSSL: |
| 184 | switch (strlen($this->key)) { |
| 185 | case 5: |
| 186 | $this->cipher_name_openssl = 'rc4-40'; |
| 187 | break; |
| 188 | case 8: |
| 189 | $this->cipher_name_openssl = 'rc4-64'; |
| 190 | break; |
| 191 | case 16: |
| 192 | $this->cipher_name_openssl = 'rc4'; |
| 193 | break; |
| 194 | default: |
| 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return parent::isValidEngine($engine); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Dummy function. |
| 204 | * |
| 205 | * Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1]. |
| 206 | * If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before |
| 207 | * calling setKey(). |
| 208 | * |
| 209 | * [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol, |
| 210 | * the IV's are relatively easy to predict, an attack described by |
| 211 | * {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir} |
| 212 | * can be used to quickly guess at the rest of the key. The following links elaborate: |
| 213 | * |
| 214 | * {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009} |
| 215 | * {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack} |
| 216 | * |
| 217 | * @param String $iv |
| 218 | * @see Crypt_RC4::setKey() |
| 219 | * @access public |
| 220 | */ |
| 221 | function setIV($iv) |
| 222 | { |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Sets the key. |
| 227 | * |
| 228 | * Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will |
| 229 | * be used. If no key is explicitly set, it'll be assumed to be a single null byte. |
| 230 | * |
| 231 | * @access public |
| 232 | * @see Crypt_Base::setKey() |
| 233 | * @param String $key |
| 234 | */ |
| 235 | function setKey($key) |
| 236 | { |
| 237 | parent::setKey(substr($key, 0, 256)); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Encrypts a message. |
| 242 | * |
| 243 | * @see Crypt_Base::decrypt() |
| 244 | * @see Crypt_RC4::_crypt() |
| 245 | * @access public |
| 246 | * @param String $plaintext |
| 247 | * @return String $ciphertext |
| 248 | */ |
| 249 | function encrypt($plaintext) |
| 250 | { |
| 251 | if ($this->engine != CRYPT_ENGINE_INTERNAL) { |
| 252 | return parent::encrypt($plaintext); |
| 253 | } |
| 254 | return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Decrypts a message. |
| 259 | * |
| 260 | * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)). |
| 261 | * At least if the continuous buffer is disabled. |
| 262 | * |
| 263 | * @see Crypt_Base::encrypt() |
| 264 | * @see Crypt_RC4::_crypt() |
| 265 | * @access public |
| 266 | * @param String $ciphertext |
| 267 | * @return String $plaintext |
| 268 | */ |
| 269 | function decrypt($ciphertext) |
| 270 | { |
| 271 | if ($this->engine != CRYPT_ENGINE_INTERNAL) { |
| 272 | return parent::decrypt($ciphertext); |
| 273 | } |
| 274 | return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /** |
| 279 | * Setup the key (expansion) |
| 280 | * |
| 281 | * @see Crypt_Base::_setupKey() |
| 282 | * @access private |
| 283 | */ |
| 284 | function _setupKey() |
| 285 | { |
| 286 | $key = $this->key; |
| 287 | $keyLength = strlen($key); |
| 288 | $keyStream = range(0, 255); |
| 289 | $j = 0; |
| 290 | for ($i = 0; $i < 256; $i++) { |
| 291 | $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255; |
| 292 | $temp = $keyStream[$i]; |
| 293 | $keyStream[$i] = $keyStream[$j]; |
| 294 | $keyStream[$j] = $temp; |
| 295 | } |
| 296 | |
| 297 | $this->stream = array(); |
| 298 | $this->stream[CRYPT_RC4_DECRYPT] = $this->stream[CRYPT_RC4_ENCRYPT] = array( |
| 299 | 0, // index $i |
| 300 | 0, // index $j |
| 301 | $keyStream |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Encrypts or decrypts a message. |
| 307 | * |
| 308 | * @see Crypt_RC4::encrypt() |
| 309 | * @see Crypt_RC4::decrypt() |
| 310 | * @access private |
| 311 | * @param String $text |
| 312 | * @param Integer $mode |
| 313 | * @return String $text |
| 314 | */ |
| 315 | function _crypt($text, $mode) |
| 316 | { |
| 317 | if ($this->changed) { |
| 318 | $this->_setup(); |
| 319 | $this->changed = false; |
| 320 | } |
| 321 | |
| 322 | $stream = &$this->stream[$mode]; |
| 323 | if ($this->continuousBuffer) { |
| 324 | $i = &$stream[0]; |
| 325 | $j = &$stream[1]; |
| 326 | $keyStream = &$stream[2]; |
| 327 | } else { |
| 328 | $i = $stream[0]; |
| 329 | $j = $stream[1]; |
| 330 | $keyStream = $stream[2]; |
| 331 | } |
| 332 | |
| 333 | $len = strlen($text); |
| 334 | for ($k = 0; $k < $len; ++$k) { |
| 335 | $i = ($i + 1) & 255; |
| 336 | $ksi = $keyStream[$i]; |
| 337 | $j = ($j + $ksi) & 255; |
| 338 | $ksj = $keyStream[$j]; |
| 339 | |
| 340 | $keyStream[$i] = $ksj; |
| 341 | $keyStream[$j] = $ksi; |
| 342 | $text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]); |
| 343 | } |
| 344 | |
| 345 | return $text; |
| 346 | } |
| 347 | } |