Rev 846 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
827 | daniel-mar | 1 | <?php |
2 | |||
3 | /** |
||
4 | * Pure-PHP implementation of AES. |
||
5 | * |
||
6 | * Uses mcrypt, 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 | * |
||
874 | daniel-mar | 42 | * @category Crypt |
43 | * @package AES |
||
827 | daniel-mar | 44 | * @author Jim Wigginton <terrafrost@php.net> |
45 | * @copyright 2008 Jim Wigginton |
||
46 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
47 | * @link http://phpseclib.sourceforge.net |
||
48 | */ |
||
49 | |||
50 | namespace phpseclib3\Crypt; |
||
51 | |||
52 | /** |
||
53 | * Pure-PHP implementation of AES. |
||
54 | * |
||
874 | daniel-mar | 55 | * @package AES |
827 | daniel-mar | 56 | * @author Jim Wigginton <terrafrost@php.net> |
874 | daniel-mar | 57 | * @access public |
827 | daniel-mar | 58 | */ |
59 | class AES extends Rijndael |
||
60 | { |
||
61 | /** |
||
62 | * Dummy function |
||
63 | * |
||
64 | * Since \phpseclib3\Crypt\AES extends \phpseclib3\Crypt\Rijndael, this function is, technically, available, but it doesn't do anything. |
||
65 | * |
||
66 | * @see \phpseclib3\Crypt\Rijndael::setBlockLength() |
||
874 | daniel-mar | 67 | * @access public |
827 | daniel-mar | 68 | * @param int $length |
69 | * @throws \BadMethodCallException anytime it's called |
||
70 | */ |
||
71 | public function setBlockLength($length) |
||
72 | { |
||
73 | throw new \BadMethodCallException('The block length cannot be set for AES.'); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Sets the key length |
||
78 | * |
||
79 | * Valid key lengths are 128, 192, and 256. Set the link to bool(false) to disable a fixed key length |
||
80 | * |
||
81 | * @see \phpseclib3\Crypt\Rijndael:setKeyLength() |
||
874 | daniel-mar | 82 | * @access public |
827 | daniel-mar | 83 | * @param int $length |
84 | * @throws \LengthException if the key length isn't supported |
||
85 | */ |
||
86 | public function setKeyLength($length) |
||
87 | { |
||
88 | switch ($length) { |
||
89 | case 128: |
||
90 | case 192: |
||
91 | case 256: |
||
92 | break; |
||
93 | default: |
||
94 | throw new \LengthException('Key of size ' . $length . ' not supported by this algorithm. Only keys of sizes 128, 192 or 256 supported'); |
||
95 | } |
||
96 | parent::setKeyLength($length); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Sets the key. |
||
101 | * |
||
102 | * Rijndael supports five different key lengths, AES only supports three. |
||
103 | * |
||
104 | * @see \phpseclib3\Crypt\Rijndael:setKey() |
||
105 | * @see setKeyLength() |
||
874 | daniel-mar | 106 | * @access public |
827 | daniel-mar | 107 | * @param string $key |
108 | * @throws \LengthException if the key length isn't supported |
||
109 | */ |
||
110 | public function setKey($key) |
||
111 | { |
||
112 | switch (strlen($key)) { |
||
113 | case 16: |
||
114 | case 24: |
||
115 | case 32: |
||
116 | break; |
||
117 | default: |
||
118 | throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported'); |
||
119 | } |
||
120 | |||
121 | parent::setKey($key); |
||
122 | } |
||
123 | } |