* 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. * * @category Crypt * @package Crypt_AES * @author Jim Wigginton * @copyright MMVIII Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version $Id: AES.php,v 1.7 2010/02/09 06:10:25 terrafrost Exp $ * @link http://phpseclib.sourceforge.net */ /** * Include Crypt_Rijndael */ if (!class_exists('Crypt_Rijndael')) { require_once 'Rijndael.php'; } /**#@+ * @access public * @see Crypt_AES::encrypt() * @see Crypt_AES::decrypt() */ /** * Encrypt / decrypt using the Counter mode. * * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode. * * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29 */ define('CRYPT_AES_MODE_CTR', CRYPT_MODE_CTR); /** * Encrypt / decrypt using the Electronic Code Book mode. * * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29 */ define('CRYPT_AES_MODE_ECB', CRYPT_MODE_ECB); /** * Encrypt / decrypt using the Code Book Chaining mode. * * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29 */ define('CRYPT_AES_MODE_CBC', CRYPT_MODE_CBC); /** * Encrypt / decrypt using the Cipher Feedback mode. * * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29 */ define('CRYPT_AES_MODE_CFB', CRYPT_MODE_CFB); /** * Encrypt / decrypt using the Cipher Feedback mode. * * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29 */ define('CRYPT_AES_MODE_OFB', CRYPT_MODE_OFB); /**#@-*/ /**#@+ * @access private * @see Crypt_AES::Crypt_AES() */ /** * Toggles the internal implementation */ define('CRYPT_AES_MODE_INTERNAL', CRYPT_MODE_INTERNAL); /** * Toggles the mcrypt implementation */ define('CRYPT_AES_MODE_MCRYPT', CRYPT_MODE_MCRYPT); /**#@-*/ /** * Pure-PHP implementation of AES. * * @author Jim Wigginton * @version 0.1.0 * @access public * @package Crypt_AES */ class Crypt_AES extends Crypt_Rijndael { /** * The namespace used by the cipher for its constants. * * @see Crypt_Base::const_namespace * @var String * @access private */ var $const_namespace = 'AES'; /** * The mcrypt specific name of the cipher * * @see Crypt_Base::cipher_name_mcrypt * @var String * @access private */ var $cipher_name_mcrypt = 'rijndael-128'; /** * Default Constructor. * * Determines whether or not the mcrypt extension should be used. * * $mode could be: * * - CRYPT_AES_MODE_ECB * * - CRYPT_AES_MODE_CBC * * - CRYPT_AES_MODE_CTR * * - CRYPT_AES_MODE_CFB * * - CRYPT_AES_MODE_OFB * * If not explictly set, CRYPT_AES_MODE_CBC will be used. * * @see Crypt_Rijndael::Crypt_Rijndael() * @see Crypt_Base::Crypt_Base() * @param optional Integer $mode * @access public */ function Crypt_AES($mode = CRYPT_AES_MODE_CBC) { parent::Crypt_Rijndael($mode); } /** * Dummy function * * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything. * * @see Crypt_Rijndael::setBlockLength() * @access public * @param Integer $length */ function setBlockLength($length) { return; } /** * 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 than 128 and invalid, it will be rounded down to the closest valid amount. * * Note: phpseclib extends AES for using 160- and 224-bit keys but they are officially not defined in AES * and the most (if not all) implementations of AES are not able using 160/224-bit keys but round/pad * them up to 192/256 bits as, for example, mcrypt will do. * * That said, if you want be compatible with other AES implementations, * you should not setKeyLength(160) or setKeyLength(224). * * Additional: In case of 160- and 224-bit keys, phpseclib will/can, for that reason, not use * the mcrypt php extention, even if available. This results then in slower encryption. * * @access public * @param Integer $length */ function setKeyLength($length) { parent::setKeyLength($length); switch ($this->key_size) { case 20: // 160-bits case 28: // 224-bits $this->engine = CRYPT_AES_MODE_INTERNAL; // because mcrypt is not able to use (real) 160/224-bit keys break; // we force using our internal AES engine instead of mcrypt. default: $this->engine = CRYPT_AES_MODE; } } /** * Setup the CRYPT_MODE_MCRYPT $engine * * Validates all the variables. * * @see Crypt_Base::_setupMcrypt() * @access private */ function _setupMcrypt() { if (!$this->explicit_key_length) { $length = strlen($this->key); switch (true) { case $length <= 16: $this->key_size = 16; break; case $length <= 24: $this->key_size = 24; break; default: $this->key_size = 32; } } $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, "\0"); parent::_setupMcrypt(); } } // vim: ts=4:sw=4:et: // vim6: fdl=1: