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 \phpseclib\Crypt\AES extends \phpseclib\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 \phpseclib\Crypt\AES(); |
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
|
|
|
* @category Crypt |
43
|
|
|
* @package AES |
44
|
|
|
* @author Jim Wigginton <[email protected]> |
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 phpseclib\Crypt; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Pure-PHP implementation of AES. |
54
|
|
|
* |
55
|
|
|
* @package AES |
56
|
|
|
* @author Jim Wigginton <[email protected]> |
57
|
|
|
* @access public |
58
|
|
|
*/ |
59
|
|
|
class AES extends Rijndael |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* Dummy function |
63
|
|
|
* |
64
|
|
|
* Since \phpseclib\Crypt\AES extends \phpseclib\Crypt\Rijndael, this function is, technically, available, but it doesn't do anything. |
65
|
|
|
* |
66
|
|
|
* @see \phpseclib\Crypt\Rijndael::setBlockLength() |
67
|
|
|
* @access public |
68
|
|
|
* @param int $length |
69
|
|
|
*/ |
70
|
|
|
public function setBlockLength($length) |
71
|
|
|
{ |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sets the key length |
77
|
|
|
* |
78
|
|
|
* Valid key lengths are 128, 192, and 256. If the length is less than 128, it will be rounded up to |
79
|
|
|
* 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount. |
80
|
|
|
* |
81
|
|
|
* @see \phpseclib\Crypt\Rijndael:setKeyLength() |
82
|
|
|
* @access public |
83
|
|
|
* @param int $length |
84
|
|
|
*/ |
85
|
|
|
public function setKeyLength($length) |
86
|
|
|
{ |
87
|
|
|
switch ($length) { |
88
|
|
|
case 160: |
89
|
|
|
$length = 192; |
90
|
|
|
break; |
91
|
|
|
case 224: |
92
|
|
|
$length = 256; |
93
|
|
|
} |
94
|
|
|
parent::setKeyLength($length); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets the key. |
99
|
|
|
* |
100
|
|
|
* Rijndael supports five different key lengths, AES only supports three. |
101
|
|
|
* |
102
|
|
|
* @see \phpseclib\Crypt\Rijndael:setKey() |
103
|
|
|
* @see setKeyLength() |
104
|
|
|
* @access public |
105
|
|
|
* @param string $key |
106
|
|
|
*/ |
107
|
|
|
public function setKey($key) |
108
|
|
|
{ |
109
|
|
|
parent::setKey($key); |
110
|
|
|
|
111
|
|
|
if (!$this->explicit_key_length) { |
112
|
|
|
$length = strlen($key); |
113
|
|
|
switch (true) { |
114
|
|
|
case $length <= 16: |
115
|
|
|
$this->key_length = 16; |
116
|
|
|
break; |
117
|
|
|
case $length <= 24: |
118
|
|
|
$this->key_length = 24; |
119
|
|
|
break; |
120
|
|
|
default: |
121
|
|
|
$this->key_length = 32; |
122
|
|
|
} |
123
|
|
|
$this->_setEngine(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|