mirror of
https://github.com/danog/tgseclib.git
synced 2025-01-22 05:51:20 +01:00
c4b103468c
Change copyright years from roman numeral to decimal numbers. * bantu/fix-547: Change copyright years from roman numeral to decimal numbers. Conflicts: phpseclib/System/SSH_Agent.php
137 lines
3.7 KiB
PHP
137 lines
3.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Pure-PHP implementation of AES.
|
|
*
|
|
* Uses mcrypt, if available/possible, 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 192-bits and 192 bits will be the key length until {@link Crypt_AES::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:
|
|
* <code>
|
|
* <?php
|
|
* include 'Crypt/AES.php';
|
|
*
|
|
* $aes = new Crypt_AES();
|
|
*
|
|
* $aes->setKey('abcdefghijklmnop');
|
|
*
|
|
* $size = 10 * 1024;
|
|
* $plaintext = '';
|
|
* for ($i = 0; $i < $size; $i++) {
|
|
* $plaintext.= 'a';
|
|
* }
|
|
*
|
|
* echo $aes->decrypt($aes->encrypt($plaintext));
|
|
* ?>
|
|
* </code>
|
|
*
|
|
* @category Crypt
|
|
* @package Crypt_AES
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
* @copyright 2008 Jim Wigginton
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
* @link http://phpseclib.sourceforge.net
|
|
*/
|
|
|
|
/**
|
|
* Include Crypt_Rijndael
|
|
*/
|
|
if (!class_exists('Crypt_Rijndael')) {
|
|
include_once 'Rijndael.php';
|
|
}
|
|
|
|
/**
|
|
* Pure-PHP implementation of AES.
|
|
*
|
|
* @package Crypt_AES
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
* @access public
|
|
*/
|
|
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';
|
|
|
|
/**
|
|
* 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, 192, 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.
|
|
*
|
|
* @see Crypt_Rijndael:setKeyLength()
|
|
* @access public
|
|
* @param Integer $length
|
|
*/
|
|
function setKeyLength($length)
|
|
{
|
|
switch ($length) {
|
|
case 160:
|
|
$length = 192;
|
|
break;
|
|
case 224:
|
|
$length = 256;
|
|
}
|
|
parent::setKeyLength($length);
|
|
}
|
|
|
|
/**
|
|
* Sets the key.
|
|
*
|
|
* Rijndael supports five different key lengths, AES only supports three.
|
|
*
|
|
* @see Crypt_Rijndael:setKey()
|
|
* @see setKeyLength()
|
|
* @access public
|
|
* @param String $key
|
|
*/
|
|
function setKey($key)
|
|
{
|
|
parent::setKey($key);
|
|
|
|
if (!$this->explicit_key_length) {
|
|
$length = strlen($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->_setupEngine();
|
|
}
|
|
}
|
|
}
|