Crypt_Rijndael()
_decryptBlock()
_encryptBlock()
_generate_xor()
_invSubWord()
_pad()
_setup()
_string_shift()
_subWord()
_unpad()
decrypt()
disableContinuousBuffer()
disablePadding()
enableContinuousBuffer()
enablePadding()
encrypt()
setBlockLength()
setIV()
setKey()
setKeyLength()
$c
$changed
$continuousBuffer
$debuffer
$decryptIV
$dt0
$dt1
$dt2
$dt3
$dw
$enbuffer
$encryptIV
$explicit_key_length
$iv
$key
$mode
$paddable
$padding
$t0
$t1
$t2
$t3
$w
Pure-PHP implementation of Rijndael.
author | Jim Wigginton |
---|---|
version | 0.1.0 |
access | public |
package | Crypt_Rijndael |
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 |
---|
\optional
Integer $mode
_decryptBlock(String $in) : String
access | private |
---|
String
String
_encryptBlock(String $in) : String
access | private |
---|
String
String
_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 |
Integer
String
_invSubWord($word)
access | private |
---|
_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 |
_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 $string, \optional $index) : String
Inspired by array_shift
access | private |
---|
String
\optional
Integer $index
String
_subWord($word)
access | private |
---|
_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 |
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 |
String
disableContinuousBuffer()
The default behavior.
see | \global\Crypt_Rijndael::enableContinuousBuffer() |
---|---|
access | public |
disablePadding()
see | \global\Crypt_Rijndael::enablePadding() |
---|---|
access | public |
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 |
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 |
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 |
String
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 |
---|
Integer
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 |
---|
String
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 |
---|
String
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 |
---|
Integer
$c : Array
access | private |
---|
$changed : Boolean
see | \global\setKey() |
---|---|
see | \global\setBlockLength() |
see | \global\setKeyLength() |
access | private |
$continuousBuffer : Boolean
see | \global\Crypt_Rijndael::enableContinuousBuffer() |
---|---|
access | private |
$debuffer : String
see | \global\Crypt_Rijndael::decrypt() |
---|---|
access | private |
$decryptIV : String
see | \global\Crypt_Rijndael::enableContinuousBuffer() |
---|---|
access | private |
$dt0 : Array
see | \global\Crypt_Rijndael() |
---|---|
access | private |
$dt1 : Array
see | \global\Crypt_Rijndael() |
---|---|
access | private |
$dt2 : Array
see | \global\Crypt_Rijndael() |
---|---|
access | private |
$dt3 : Array
see | \global\Crypt_Rijndael() |
---|---|
access | private |
$dw : Array
see | \global\_setup() |
---|---|
access | private |
$enbuffer : String
see | \global\Crypt_Rijndael::encrypt() |
---|---|
access | private |
$encryptIV : String
see | \global\Crypt_Rijndael::enableContinuousBuffer() |
---|---|
access | private |
$explicit_key_length : Boolean
see | \global\setKeyLength() |
---|---|
access | private |
$iv : String
see | \global\Crypt_Rijndael::setIV() |
---|---|
access | private |
$key : String
see | \global\Crypt_Rijndael::setKey() |
---|---|
access | private |
$mode : Integer
see | \global\Crypt_Rijndael::Crypt_Rijndael() |
---|---|
access | private |
$paddable : Boolean
see | \global\Crypt_Rijndael::Crypt_Rijndael() |
---|---|
access | private |
$padding : Boolean
see | \global\Crypt_Rijndael::enablePadding() |
---|---|
access | private |
$t0 : Array
see | \global\Crypt_Rijndael() |
---|---|
access | private |
$t1 : Array
see | \global\Crypt_Rijndael() |
---|---|
access | private |
$t2 : Array
see | \global\Crypt_Rijndael() |
---|---|
access | private |
$t3 : Array
see | \global\Crypt_Rijndael() |
---|---|
access | private |
$w : Array
see | \global\_setup() |
---|---|
access | private |