public
Crypt_Rijndael
|
#
Crypt_Rijndael( optional $mode = CRYPT_RIJNDAEL_MODE_CBC )
Default Constructor.
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.
Parameters
- $mode
optional Integer $mode
Returns
|
public
|
#
setKey( String $key )
Sets the 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.
Parameters
|
public
|
#
setIV( String $iv )
Sets the initialization vector. (optional)
Sets the initialization vector. (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.
Parameters
|
public
|
#
setKeyLength( Integer $length )
Sets the key 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.
Parameters
|
public
|
#
setPassword( String $password, optional $method = 'pbkdf2' )
Sets the password.
Depending on what $method is set to, setPassword()'s (optional) parameters
are as follows: pbkdf2: $hash, $salt, $count Set $dkLen by calling setKeyLength()
Parameters
- $password
String $password
- $method
optional String $method
|
public
|
#
setBlockLength( Integer $length )
Sets the block 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.
Parameters
|
public
|
#
_generate_xor( Integer $length, String & $iv )
Generate CTR XOR encryption key
Generate CTR XOR encryption key
Encrypt the output of this and XOR it against the ciphertext / plaintext to
get the plaintext / ciphertext in CTR mode.
Parameters
- $length
Integer $length
- $iv
String $iv
See
|
public
|
#
encrypt( String $plaintext )
Encrypts a message.
$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.
Parameters
- $plaintext
String $plaintext
See
|
public
|
#
decrypt( String $ciphertext )
Decrypts a message.
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.
Parameters
- $ciphertext
String $ciphertext
See
|
public
String
|
#
_encryptBlock( String $in )
Encrypts a block
Parameters
Returns
String
|
public
String
|
#
_decryptBlock( String $in )
Decrypts a block
Parameters
Returns
String
|
public
|
#
_setup( )
Setup Rijndael
Validates all the variables and calculates $Nr - the number of rounds that
need to be performed - and $w - the key key schedule.
|
public
|
#
_subWord( mixed $word )
Performs S-Box substitutions
Performs S-Box substitutions
|
public
|
#
_invSubWord( mixed $word )
Performs inverse S-Box substitutions
Performs inverse S-Box substitutions
|
public
|
#
enablePadding( )
Pad "packets".
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
|
public
|
|
public
|
#
_pad( mixed $text )
Pads a string
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
|
public
|
#
_unpad( mixed $text )
Unpads a string.
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
|
public
|
#
enableContinuousBuffer( )
Treat consecutive "packets" as if they are a continuous buffer.
Treat consecutive "packets" as if they are a continuous buffer.
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
|
public
|
#
disableContinuousBuffer( )
Treat consecutive packets as if they are a discontinuous buffer.
Treat consecutive packets as if they are a discontinuous buffer.
The default behavior.
See
|
public
String
|
#
_string_shift( String & $string, optional $index = 1 )
String Shift
Inspired by array_shift
Parameters
- $string
String $string
- $index
optional Integer $index
Returns
String
|
public
Integer
|
$mode |
|
public
String
|
$key |
|
public
String
|
$iv |
#
The Initialization Vector
The Initialization Vector
See
|
public
String
|
$encryptIV |
#
A "sliding" Initialization Vector
A "sliding" Initialization Vector
See
|
public
String
|
$decryptIV |
#
A "sliding" Initialization Vector
A "sliding" Initialization Vector
See
|
public
Boolean
|
$continuousBuffer |
#
Continuous Buffer status
See
|
public
Boolean
|
$padding |
|
public
Boolean
|
$changed |
#
Does the key schedule need to be (re)calculated?
Does the key schedule need to be (re)calculated?
See
|
public
Boolean
|
$explicit_key_length |
#
Has the key length explicitly been set or should it be derived from the key,
itself?
Has the key length explicitly been set or should it be derived from the key,
itself?
See
|
public
Array
|
$w |
|
public
Array
|
$dw |
#
The Inverse Key Schedule
See
|
public
Integer
|
$block_size |
|
public
Integer
|
$Nb |
#
The Block Length divided by 32
The Block Length divided by 32
See
|
public
Integer
|
$key_size |
|
public
Integer
|
$Nk |
#
The Key Length divided by 32
The Key Length divided by 32
See
|
public
Integer
|
$Nr |
|
public
Array
|
$c |
|
public
Array
|
$t0 |
#
Precomputed mixColumns table
Precomputed mixColumns table
See
|
public
Array
|
$t1 |
#
Precomputed mixColumns table
Precomputed mixColumns table
See
|
public
Array
|
$t2 |
#
Precomputed mixColumns table
Precomputed mixColumns table
See
|
public
Array
|
$t3 |
#
Precomputed mixColumns table
Precomputed mixColumns table
See
|
public
Array
|
$dt0 |
#
Precomputed invMixColumns table
Precomputed invMixColumns table
See
|
public
Array
|
$dt1 |
#
Precomputed invMixColumns table
Precomputed invMixColumns table
See
|
public
Array
|
$dt2 |
#
Precomputed invMixColumns table
Precomputed invMixColumns table
See
|
public
Array
|
$dt3 |
#
Precomputed invMixColumns table
Precomputed invMixColumns table
See
|
public
Boolean
|
$paddable |
#
Is the mode one that is paddable?
Is the mode one that is paddable?
See
|
public
String
|
$enbuffer |
#
Encryption buffer for CTR, OFB and CFB modes
Encryption buffer for CTR, OFB and CFB modes
See
|
public
String
|
$debuffer |
#
Decryption buffer for CTR, OFB and CFB modes
Decryption buffer for CTR, OFB and CFB modes
See
|