Uses mcrypt, if available, and an internal implementation, otherwise.
PHP versions 4 and 5
If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
{@link Crypt_AES::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits
it'll be null-padded to 160-bits and 160 bits will be the key length until {@link Crypt_Rijndael::setKey() setKey()}
is called, again, at which point, it'll be recalculated.
Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't
make a whole lot of sense. {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance. Calling that function,
however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
Here's a short example of how to use this library:
setKey('abcdefghijklmnop');
$size = 10 * 1024;
$plaintext = '';
for ($i = 0; $i < $size; $i++) {
$plaintext.= 'a';
}
echo $aes->decrypt($aes->encrypt($plaintext));
?>
LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Constants
CRYPT_AES_MODE_CTR
= -1
Encrypt / decrypt using the Counter mode. Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
CRYPT_AES_MODE_ECB
= 1
Encrypt / decrypt using the Electronic Code Book mode.
CRYPT_AES_MODE_CBC
= 2
Encrypt / decrypt using the Code Book Chaining mode.
Default Constructor. Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
CRYPT_AES_MODE_ECB or CRYPT_AES_MODE_CBC. If not explictly set, CRYPT_AES_MODE_CBC will be used.
Arguments
Name
Type
Description
Default
$mode
n/a
CRYPT_AES_MODE_CBC
Return value
Type
Description
\Crypt_AES
Tags
Name
Description
access
public
_decryptBlock(
String
$in,
)
:
String
Description
Decrypts a block Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
Arguments
Name
Type
Description
Default
$in
String
Return value
Type
Description
String
Tags
Name
Description
see
access
private
_encryptBlock(
String
$in,
)
:
String
Description
Encrypts a block Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
Arguments
Name
Type
Description
Default
$in
String
Return value
Type
Description
String
Tags
Name
Description
see
access
private
_mcryptSetup(
)
:
n/a
Description
Setup mcrypt Validates all the variables.
Return value
Type
Description
n/a
n/a
Tags
Name
Description
access
private
decrypt(
String
$ciphertext,
)
:
n/a
Description
Decrypts a message. If strlen($ciphertext) is not a multiple of 16, null bytes will be added to the end of the string until it is.
Arguments
Name
Type
Description
Default
$ciphertext
String
Return value
Type
Description
n/a
n/a
Tags
Name
Description
see
access
public
encrypt(
String
$plaintext,
)
:
n/a
Description
Encrypts a message. $plaintext will be padded with up to 16 additional bytes. Other AES 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:
{@link http://www.di-mgt.com.au/cryptopad.html 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 16, however, arbitrary values can be added to make it that
length.
Arguments
Name
Type
Description
Default
$plaintext
String
Return value
Type
Description
n/a
n/a
Tags
Name
Description
see
access
public
setBlockLength(
Integer
$length,
)
:
n/a
Description
Dummy function Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
mcrypt resource for decryption The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
String
public
$ecb
=
mcrypt resource for CFB mode
String
public
$enmcrypt
=
mcrypt resource for encryption The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.