Common
9 months ago
DH
9 months ago
DSA
9 months ago
EC
9 months ago
RSA
9 months ago
.htaccess
9 months ago
AES.php
9 months ago
Blowfish.php
9 months ago
ChaCha20.php
9 months ago
DES.php
9 months ago
DH.php
9 months ago
DSA.php
9 months ago
EC.php
9 months ago
Hash.php
9 months ago
PublicKeyLoader.php
9 months ago
RC2.php
9 months ago
RC4.php
9 months ago
RSA.php
9 months ago
Random.php
9 months ago
Rijndael.php
9 months ago
Salsa20.php
9 months ago
TripleDES.php
9 months ago
Twofish.php
9 months ago
index.html
9 months ago
web.config
9 months ago
AES.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Pure-PHP implementation of AES. |
| 5 | * |
| 6 | * Uses OpenSSL, if available/possible, and an internal implementation, otherwise |
| 7 | * |
| 8 | * PHP version 5 |
| 9 | * |
| 10 | * NOTE: Since AES.php is (for compatibility and phpseclib-historical reasons) virtually |
| 11 | * just a wrapper to Rijndael.php you may consider using Rijndael.php instead of |
| 12 | * to save one include_once(). |
| 13 | * |
| 14 | * If {@link self::setKeyLength() setKeyLength()} isn't called, it'll be calculated from |
| 15 | * {@link self::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits |
| 16 | * it'll be null-padded to 192-bits and 192 bits will be the key length until {@link self::setKey() setKey()} |
| 17 | * is called, again, at which point, it'll be recalculated. |
| 18 | * |
| 19 | * Since \phpseclib3\Crypt\AES extends \phpseclib3\Crypt\Rijndael, some functions are available to be called that, in the context of AES, don't |
| 20 | * make a whole lot of sense. {@link self::setBlockLength() setBlockLength()}, for instance. Calling that function, |
| 21 | * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one). |
| 22 | * |
| 23 | * Here's a short example of how to use this library: |
| 24 | * <code> |
| 25 | * <?php |
| 26 | * include 'vendor/autoload.php'; |
| 27 | * |
| 28 | * $aes = new \phpseclib3\Crypt\AES('ctr'); |
| 29 | * |
| 30 | * $aes->setKey('abcdefghijklmnop'); |
| 31 | * |
| 32 | * $size = 10 * 1024; |
| 33 | * $plaintext = ''; |
| 34 | * for ($i = 0; $i < $size; $i++) { |
| 35 | * $plaintext.= 'a'; |
| 36 | * } |
| 37 | * |
| 38 | * echo $aes->decrypt($aes->encrypt($plaintext)); |
| 39 | * ?> |
| 40 | * </code> |
| 41 | * |
| 42 | * @author Jim Wigginton <terrafrost@php.net> |
| 43 | * @copyright 2008 Jim Wigginton |
| 44 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 45 | * @link http://phpseclib.sourceforge.net |
| 46 | */ |
| 47 | |
| 48 | declare(strict_types=1); |
| 49 | |
| 50 | namespace phpseclib3\Crypt; |
| 51 | |
| 52 | use phpseclib3\Exception\BadMethodCallException; |
| 53 | use phpseclib3\Exception\LengthException; |
| 54 | |
| 55 | /** |
| 56 | * Pure-PHP implementation of AES. |
| 57 | * |
| 58 | * @author Jim Wigginton <terrafrost@php.net> |
| 59 | */ |
| 60 | class AES extends Rijndael |
| 61 | { |
| 62 | /** |
| 63 | * Dummy function |
| 64 | * |
| 65 | * Since \phpseclib3\Crypt\AES extends \phpseclib3\Crypt\Rijndael, this function is, technically, available, but it doesn't do anything. |
| 66 | * |
| 67 | * @throws BadMethodCallException anytime it's called |
| 68 | * @see \phpseclib3\Crypt\Rijndael::setBlockLength() |
| 69 | */ |
| 70 | public function setBlockLength(int $length): void |
| 71 | { |
| 72 | throw new BadMethodCallException('The block length cannot be set for AES.'); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Sets the key length |
| 77 | * |
| 78 | * Valid key lengths are 128, 192, and 256. Set the link to bool(false) to disable a fixed key length |
| 79 | * |
| 80 | * @throws LengthException if the key length isn't supported |
| 81 | * @see \phpseclib3\Crypt\Rijndael:setKeyLength() |
| 82 | */ |
| 83 | public function setKeyLength(int $length): void |
| 84 | { |
| 85 | switch ($length) { |
| 86 | case 128: |
| 87 | case 192: |
| 88 | case 256: |
| 89 | break; |
| 90 | default: |
| 91 | throw new LengthException('Key of size ' . $length . ' not supported by this algorithm. Only keys of sizes 128, 192 or 256 supported'); |
| 92 | } |
| 93 | parent::setKeyLength($length); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Sets the key. |
| 98 | * |
| 99 | * Rijndael supports five different key lengths, AES only supports three. |
| 100 | * |
| 101 | * @throws LengthException if the key length isn't supported |
| 102 | * @see \phpseclib3\Crypt\Rijndael:setKey() |
| 103 | * @see setKeyLength() |
| 104 | */ |
| 105 | public function setKey(string $key): void |
| 106 | { |
| 107 | switch (strlen($key)) { |
| 108 | case 16: |
| 109 | case 24: |
| 110 | case 32: |
| 111 | break; |
| 112 | default: |
| 113 | throw new LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported'); |
| 114 | } |
| 115 | |
| 116 | parent::setKey($key); |
| 117 | } |
| 118 | } |
| 119 |