4 * Pure-PHP implementation of AES.
6 * Uses mcrypt, if available/possible, and an internal implementation, otherwise.
10 * If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
11 * {@link Crypt_AES::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits
12 * it'll be null-padded to 192-bits and 192 bits will be the key length until {@link Crypt_AES::setKey() setKey()}
13 * is called, again, at which point, it'll be recalculated.
15 * Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't
16 * make a whole lot of sense. {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance. Calling that function,
17 * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
19 * Here's a short example of how to use this library:
22 * include 'Crypt/AES.php';
24 * $aes = new Crypt_AES();
26 * $aes->setKey('abcdefghijklmnop');
30 * for ($i = 0; $i < $size; $i++) {
34 * echo $aes->decrypt($aes->encrypt($plaintext));
38 * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
39 * of this software and associated documentation files (the "Software"), to deal
40 * in the Software without restriction, including without limitation the rights
41 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42 * copies of the Software, and to permit persons to whom the Software is
43 * furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
53 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
58 * @author Jim Wigginton <terrafrost@php.net>
59 * @copyright 2008 Jim Wigginton
60 * @license http://www.opensource.org/licenses/mit-license.html MIT License
61 * @link http://phpseclib.sourceforge.net
65 * Include Crypt_Rijndael
67 if (!class_exists('Crypt_Rijndael')) {
68 include_once 'Rijndael.php';
73 * @see Crypt_AES::encrypt()
74 * @see Crypt_AES::decrypt()
77 * Encrypt / decrypt using the Counter mode.
79 * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
81 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
83 define('CRYPT_AES_MODE_CTR', CRYPT_MODE_CTR);
85 * Encrypt / decrypt using the Electronic Code Book mode.
87 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
89 define('CRYPT_AES_MODE_ECB', CRYPT_MODE_ECB);
91 * Encrypt / decrypt using the Code Book Chaining mode.
93 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
95 define('CRYPT_AES_MODE_CBC', CRYPT_MODE_CBC);
97 * Encrypt / decrypt using the Cipher Feedback mode.
99 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
101 define('CRYPT_AES_MODE_CFB', CRYPT_MODE_CFB);
103 * Encrypt / decrypt using the Cipher Feedback mode.
105 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
107 define('CRYPT_AES_MODE_OFB', CRYPT_MODE_OFB);
112 * @see Crypt_Base::Crypt_Base()
115 * Toggles the internal implementation
117 define('CRYPT_AES_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
119 * Toggles the mcrypt implementation
121 define('CRYPT_AES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
125 * Pure-PHP implementation of AES.
128 * @author Jim Wigginton <terrafrost@php.net>
131 class Crypt_AES extends Crypt_Rijndael
134 * The namespace used by the cipher for its constants.
136 * @see Crypt_Base::const_namespace
140 var $const_namespace = 'AES';
145 * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
147 * @see Crypt_Rijndael::setBlockLength()
149 * @param Integer $length
151 function setBlockLength($length)
157 * Sets the key length
159 * Valid key lengths are 128, 192, and 256. If the length is less than 128, it will be rounded up to
160 * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount.
162 * @see Crypt_Rijndael:setKeyLength()
164 * @param Integer $length
166 function setKeyLength($length)
175 parent::setKeyLength($length);
181 * Rijndael supports five different key lengths, AES only supports three.
183 * @see Crypt_Rijndael:setKey()
184 * @see setKeyLength()
188 function setKey($key)
190 parent::setKey($key);
192 if (!$this->explicit_key_length) {
193 $length = strlen($key);
196 $this->key_size = 16;
199 $this->key_size = 24;
202 $this->key_size = 32;
204 $this->_setupEngine();