2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Pure-PHP implementation of AES.
7 * Uses mcrypt, if available/possible, and an internal implementation, otherwise.
11 * If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
12 * {@link Crypt_AES::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits
13 * it'll be null-padded to 192-bits and 192 bits will be the key length until {@link Crypt_AES::setKey() setKey()}
14 * is called, again, at which point, it'll be recalculated.
16 * Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't
17 * make a whole lot of sense. {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance. Calling that function,
18 * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
20 * Here's a short example of how to use this library:
23 * include('Crypt/AES.php');
25 * $aes = new Crypt_AES();
27 * $aes->setKey('abcdefghijklmnop');
31 * for ($i = 0; $i < $size; $i++) {
35 * echo $aes->decrypt($aes->encrypt($plaintext));
39 * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
40 * of this software and associated documentation files (the "Software"), to deal
41 * in the Software without restriction, including without limitation the rights
42 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43 * copies of the Software, and to permit persons to whom the Software is
44 * furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
59 * @author Jim Wigginton <terrafrost@php.net>
60 * @copyright MMVIII Jim Wigginton
61 * @license http://www.opensource.org/licenses/mit-license.html MIT License
62 * @link http://phpseclib.sourceforge.net
66 * Include Crypt_Rijndael
68 if (!class_exists('Crypt_Rijndael')) {
69 require_once('Rijndael.php');
74 * @see Crypt_AES::encrypt()
75 * @see Crypt_AES::decrypt()
78 * Encrypt / decrypt using the Counter mode.
80 * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
82 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
84 define('CRYPT_AES_MODE_CTR', CRYPT_MODE_CTR);
86 * Encrypt / decrypt using the Electronic Code Book mode.
88 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
90 define('CRYPT_AES_MODE_ECB', CRYPT_MODE_ECB);
92 * Encrypt / decrypt using the Code Book Chaining mode.
94 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
96 define('CRYPT_AES_MODE_CBC', CRYPT_MODE_CBC);
98 * Encrypt / decrypt using the Cipher Feedback mode.
100 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
102 define('CRYPT_AES_MODE_CFB', CRYPT_MODE_CFB);
104 * Encrypt / decrypt using the Cipher Feedback mode.
106 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
108 define('CRYPT_AES_MODE_OFB', CRYPT_MODE_OFB);
113 * @see Crypt_AES::Crypt_AES()
116 * Toggles the internal implementation
118 define('CRYPT_AES_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
120 * Toggles the mcrypt implementation
122 define('CRYPT_AES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
126 * Pure-PHP implementation of AES.
128 * @author Jim Wigginton <terrafrost@php.net>
133 class Crypt_AES extends Crypt_Rijndael {
135 * The namespace used by the cipher for its constants.
137 * @see Crypt_Base::const_namespace
141 var $const_namespace = 'AES';
144 * Default Constructor.
146 * Determines whether or not the mcrypt extension should be used.
150 * - CRYPT_AES_MODE_ECB
152 * - CRYPT_AES_MODE_CBC
154 * - CRYPT_AES_MODE_CTR
156 * - CRYPT_AES_MODE_CFB
158 * - CRYPT_AES_MODE_OFB
160 * If not explictly set, CRYPT_AES_MODE_CBC will be used.
162 * @see Crypt_Rijndael::Crypt_Rijndael()
163 * @see Crypt_Base::Crypt_Base()
164 * @param optional Integer $mode
167 function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
169 parent::Crypt_Rijndael($mode);
175 * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
177 * @see Crypt_Rijndael::setBlockLength()
179 * @param Integer $length
181 function setBlockLength($length)
187 // vim: ts=4:sw=4:et: