Pure-PHP implementation of RC4.

author | Jim Wigginton
version | 0.1.0
access | public
package | Crypt_RC4

### Methods

## Default Constructor.

    Crypt_RC4() : \Crypt_RC4

Determines whether or not the mcrypt extension should be used.

access | public

### Returns

`\Crypt_RC4`

## Class destructor.

    __destruct()

Will be called, automatically, if you're using PHP5. If you're using PHP4, call it yourself. Only really needs to be called if mcrypt is being used.

access | public

## Encrypts or decrypts a message.

    _crypt(String $text, Integer $mode)

see | \global\Crypt_RC4::encrypt()
see | \global\Crypt_RC4::decrypt()
access | private

### Parameters

#### $text

`String`

#### $mode

`Integer`

## Decrypts a message.

    decrypt(String $ciphertext)

$this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)). Atleast if the continuous buffer is disabled.

see | \global\Crypt_RC4::_crypt()
access | public

### Parameters

#### $ciphertext

`String`

## Treat consecutive packets as if they are a discontinuous buffer.

    disableContinuousBuffer()

The default behavior.

see | \global\Crypt_RC4::enableContinuousBuffer()
access | public

## Dummy function.

    disablePadding()

see | \global\Crypt_RC4::enablePadding()
access | public

## Treat consecutive "packets" as if they are a continuous buffer.

    enableContinuousBuffer()

Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets will yield different outputs:

       echo $rc4->encrypt(substr($plaintext, 0, 8));
       echo $rc4->encrypt(substr($plaintext, 8, 8));

       echo $rc4->encrypt($plaintext);

The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates another, as demonstrated with the following:

       $rc4->encrypt(substr($plaintext, 0, 8));
       echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));

       echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));

With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different outputs. The reason is due to the fact that the initialization vector's change after every encryption / decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.

Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them), however, they are also less intuitive and more likely to cause you problems.

see | \global\Crypt_RC4::disableContinuousBuffer()
access | public

## Dummy function.

    enablePadding()

Since RC4 is a stream cipher and not a block cipher, no padding is necessary. The only reason this function is included is so that you can switch between a block cipher and a stream cipher transparently.

see | \global\Crypt_RC4::disablePadding()
access | public

## Encrypts a message.

    encrypt(String $plaintext)

see | \global\Crypt_RC4::_crypt()
access | public

### Parameters

#### $plaintext

`String`

## Dummy function.

    setIV(String $iv)

Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1]. If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before calling setKey().

[1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol, the IV's are relatively easy to predict, an attack described by Scott Fluhrer, Itsik Mantin, and Adi Shamir can be used to quickly guess at the rest of the key. The following links elaborate:

http://www.rsa.com/rsalabs/node.asp?id=2009 http://en.wikipedia.org/wiki/Related_key_attack

see | \global\Crypt_RC4::setKey()
access | public

### Parameters

#### $iv

`String`

## Sets the key.

    setKey(String $key)

Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will be used. If no key is explicitly set, it'll be assumed to be a single null byte.

access | public

### Parameters

#### $key

`String`

## Sets MCrypt parameters.

    setMCrypt(\optional $algorithm_directory, \optional $mode_directory)

(optional)

If MCrypt is being used, empty strings will be used, unless otherwise specified.

link | http://php.net/function.mcrypt-module-open#function.mcrypt-module-open
access | public

### Parameters

#### $algorithm_directory

`\optional`

Integer $algorithm_directory

#### $mode_directory

`\optional`

Integer $mode_directory

### Properties

## Continuous Buffer status

    $continuousBuffer : Boolean

see | \global\Crypt_RC4::enableContinuousBuffer()
access | private

## The $i and $j indexes for decryption

    $decryptIndex : Integer

see | \global\Crypt_RC4::_crypt()
access | private

## The Key Stream for decryption

    $decryptStream : Array

If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object

see | \global\Crypt_RC4::setKey()
access | private

## The $i and $j indexes for encryption

    $encryptIndex : Integer

see | \global\Crypt_RC4::_crypt()
access | private

## The Key Stream for encryption

    $encryptStream : Array

If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object

see | \global\Crypt_RC4::setKey()
access | private

## The Key

    $key : String

see | \global\Crypt_RC4::setKey()
access | private

## MCrypt parameters

    $mcrypt : Array

see | \global\Crypt_RC4::setMCrypt()
access | private

## The Encryption Algorithm

    $mode : Integer

Only used if CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT. Only possible values are MCRYPT_RC4 or MCRYPT_ARCFOUR.

see | \global\Crypt_RC4::Crypt_RC4()
access | private