Pure-PHP implementation of Rijndael.

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

### Methods

## Default Constructor.

    Crypt_Rijndael(\optional $mode) : \Crypt_Rijndael

Determines whether or not the mcrypt extension should be used. $mode should only, at present, be CRYPT_RIJNDAEL_MODE_ECB or CRYPT_RIJNDAEL_MODE_CBC. If not explictly set, CRYPT_RIJNDAEL_MODE_CBC will be used.

access | public

### Parameters

#### $mode

`\optional`

Integer $mode

### Returns

`\Crypt_Rijndael`

## Decrypts a block

    _decryptBlock(String $in) : String

access | private

### Parameters

#### $in

`String`

### Returns

`String`

## Encrypts a block

    _encryptBlock(String $in) : String

access | private

### Parameters

#### $in

`String`

### Returns

`String`

## Generate CTR XOR encryption key

    _generate_xor(Integer $length, String $iv)

Encrypt the output of this and XOR it against the ciphertext / plaintext to get the plaintext / ciphertext in CTR mode.

see | \global\Crypt_Rijndael::decrypt()
see | \global\Crypt_Rijndael::encrypt()
access | public

### Parameters

#### $length

`Integer`

#### $iv

`String`

## Performs inverse S-Box substitutions

    _invSubWord($word)

access | private

### Parameters

#### $word

## Pads a string

    _pad($text)

Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize. $block_size - (strlen($text) % $block_size) bytes are added, each of which is equal to chr($block_size - (strlen($text) % $block_size)

If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless and padding will, hence forth, be enabled.

see | \global\Crypt_Rijndael::_unpad()
access | private

### Parameters

#### $text

## Setup Rijndael

    _setup()

Validates all the variables and calculates $Nr - the number of rounds that need to be performed - and $w - the key key schedule.

access | private

## String Shift

    _string_shift(String $string, \optional $index) : String

Inspired by array_shift

access | private

### Parameters

#### $string

`String`

#### $index

`\optional`

Integer $index

### Returns

`String`

## Performs S-Box substitutions

    _subWord($word)

access | private

### Parameters

#### $word

## Unpads a string.

    _unpad($text)

If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong and false will be returned.

see | \global\Crypt_Rijndael::_pad()
access | private

### Parameters

#### $text

## Decrypts a message.

    decrypt(String $ciphertext)

If strlen($ciphertext) is not a multiple of the block size, null bytes will be added to the end of the string until it is.

see | \global\Crypt_Rijndael::encrypt()
access | public

### Parameters

#### $ciphertext

`String`

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

    disableContinuousBuffer()

The default behavior.

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

## Do not pad packets.

    disablePadding()

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

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

    enableContinuousBuffer()

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

       echo $rijndael->encrypt(substr($plaintext,  0, 16));
       echo $rijndael->encrypt(substr($plaintext, 16, 16));

       echo $rijndael->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:

       $rijndael->encrypt(substr($plaintext, 0, 16));
       echo $rijndael->decrypt($des->encrypt(substr($plaintext, 16, 16)));

       echo $rijndael->decrypt($des->encrypt(substr($plaintext, 16, 16)));

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_Rijndael() 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_Rijndael::disableContinuousBuffer()
access | public

## Pad "packets".

    enablePadding()

Rijndael works by encrypting between sixteen and thirty-two bytes at a time, provided that number is also a multiple of four. If you ever need to encrypt or decrypt something that isn't of the proper length, it becomes necessary to pad the input so that it is of the proper length.

Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH, where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is transmitted separately)

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

## Encrypts a message.

    encrypt(String $plaintext)

$plaintext will be padded with additional bytes such that it's length is a multiple of the block size. Other Rjindael implementations may or may not pad in the same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following URL:

http://www.di-mgt.com.au/cryptopad.html

An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does. strlen($plaintext) will still need to be a multiple of 8, however, arbitrary values can be added to make it that length.

see | \global\Crypt_Rijndael::decrypt()
access | public

### Parameters

#### $plaintext

`String`

## Sets the block length

    setBlockLength(Integer $length)

Valid block lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to 128. If the length is greater then 128 and invalid, it will be rounded down to the closest valid amount.

access | public

### Parameters

#### $length

`Integer`

## Sets the initialization vector.

    setIV(String $iv)

(optional)

SetIV is not required when CRYPT_RIJNDAEL_MODE_ECB is being used. If not explictly set, it'll be assumed to be all zero's.

access | public

### Parameters

#### $iv

`String`

## Sets the key.

    setKey(String $key)

Keys can be of any length. Rijndael, itself, requires the use of a key that's between 128-bits and 256-bits long and whose length is a multiple of 32. If the key is less than 256-bits and the key length isn't set, we round the length up to the closest valid key length, padding $key with null bytes. If the key is more than 256-bits, we trim the excess bits.

If the key is not explicitly set, it'll be assumed to be all null bytes.

access | public

### Parameters

#### $key

`String`

## Sets the key length

    setKeyLength(Integer $length)

Valid key lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to 128. If the length is greater then 128 and invalid, it will be rounded down to the closest valid amount.

access | public

### Parameters

#### $length

`Integer`

### Properties

## Shift offsets

    $c : Array

access | private

## Does the key schedule need to be (re)calculated?

    $changed : Boolean

see | \global\setKey()
see | \global\setBlockLength()
see | \global\setKeyLength()
access | private

## Continuous Buffer status

    $continuousBuffer : Boolean

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

## Decryption buffer for CTR, OFB and CFB modes

    $debuffer : String

see | \global\Crypt_Rijndael::decrypt()
access | private

## A "sliding" Initialization Vector

    $decryptIV : String

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

## Precomputed invMixColumns table

    $dt0 : Array

see | \global\Crypt_Rijndael()
access | private

## Precomputed invMixColumns table

    $dt1 : Array

see | \global\Crypt_Rijndael()
access | private

## Precomputed invMixColumns table

    $dt2 : Array

see | \global\Crypt_Rijndael()
access | private

## Precomputed invMixColumns table

    $dt3 : Array

see | \global\Crypt_Rijndael()
access | private

## The Inverse Key Schedule

    $dw : Array

see | \global\_setup()
access | private

## Encryption buffer for CTR, OFB and CFB modes

    $enbuffer : String

see | \global\Crypt_Rijndael::encrypt()
access | private

## A "sliding" Initialization Vector

    $encryptIV : String

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

## Has the key length explicitly been set or should it be derived from the key, itself?

    $explicit_key_length : Boolean

see | \global\setKeyLength()
access | private

## The Initialization Vector

    $iv : String

see | \global\Crypt_Rijndael::setIV()
access | private

## The Key

    $key : String

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

## The Encryption Mode

    $mode : Integer

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

## Is the mode one that is paddable?

    $paddable : Boolean

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

## Padding status

    $padding : Boolean

see | \global\Crypt_Rijndael::enablePadding()
access | private

## Precomputed mixColumns table

    $t0 : Array

see | \global\Crypt_Rijndael()
access | private

## Precomputed mixColumns table

    $t1 : Array

see | \global\Crypt_Rijndael()
access | private

## Precomputed mixColumns table

    $t2 : Array

see | \global\Crypt_Rijndael()
access | private

## Precomputed mixColumns table

    $t3 : Array

see | \global\Crypt_Rijndael()
access | private

## The Key Schedule

    $w : Array

see | \global\_setup()
access | private