1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-12-03 18:17:49 +01:00
tgseclib/phpseclib/Crypt/AES.php

129 lines
3.6 KiB
PHP
Raw Normal View History

<?php
/**
* Pure-PHP implementation of AES.
*
* Uses mcrypt, if available/possible, and an internal implementation, otherwise.
*
2015-04-02 12:57:52 +02:00
* PHP version 5
*
2015-07-15 03:52:31 +02:00
* NOTE: Since AES.php is (for compatibility and phpseclib-historical reasons) virtually
* just a wrapper to Rijndael.php you may consider using Rijndael.php instead of
* to save one include_once().
*
* If {@link self::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
* {@link self::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 self::setKey() setKey()}
* is called, again, at which point, it'll be recalculated.
*
2014-12-17 01:16:54 +01:00
* Since \phpseclib\Crypt\AES extends \phpseclib\Crypt\Rijndael, some functions are available to be called that, in the context of AES, don't
* make a whole lot of sense. {@link self::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
2014-12-17 01:16:54 +01:00
* include 'vendor/autoload.php';
*
2014-12-17 01:16:54 +01:00
* $aes = new \phpseclib\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
2014-12-17 01:16:54 +01:00
* @package 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
*/
2014-12-17 01:16:54 +01:00
namespace phpseclib\Crypt;
2016-04-28 22:34:10 +02:00
use phpseclib\Crypt\Rijndael;
/**
* Pure-PHP implementation of AES.
*
2014-12-17 01:16:54 +01:00
* @package AES
* @author Jim Wigginton <terrafrost@php.net>
* @access public
*/
2014-12-17 01:16:54 +01:00
class AES extends Rijndael
{
/**
* Dummy function
*
2014-12-17 01:16:54 +01:00
* Since \phpseclib\Crypt\AES extends \phpseclib\Crypt\Rijndael, this function is, technically, available, but it doesn't do anything.
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Rijndael::setBlockLength()
* @access public
* @param int $length
*/
function setBlockLength($length)
{
2016-04-28 22:34:10 +02:00
return;
}
/**
* Sets the key length
*
2016-04-28 22:34:10 +02:00
* 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.
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Rijndael:setKeyLength()
* @access public
* @param int $length
*/
function setKeyLength($length)
{
switch ($length) {
2016-04-28 22:34:10 +02:00
case 160:
$length = 192;
break;
2016-04-28 22:34:10 +02:00
case 224:
$length = 256;
}
parent::setKeyLength($length);
}
/**
* Sets the key.
*
2014-08-15 06:09:17 +02:00
* Rijndael supports five different key lengths, AES only supports three.
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Rijndael:setKey()
* @see setKeyLength()
* @access public
* @param string $key
*/
function setKey($key)
{
parent::setKey($key);
2016-04-28 22:34:10 +02:00
if (!$this->explicit_key_length) {
$length = strlen($key);
switch (true) {
case $length <= 16:
$this->key_length = 16;
break;
case $length <= 24:
$this->key_length = 24;
break;
default:
$this->key_length = 32;
}
$this->_setEngine();
}
}
}