Skip to content

Commit e2ec668

Browse files
committed
Adds phpseclib 0.3.9.
1 parent f380833 commit e2ec668

24 files changed

+32239
-0
lines changed

tools/phpseclib0.3.9/Crypt/AES.php

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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 versions 4 and 5
9+
*
10+
* If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
11+
* {@link Crypt_AES::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits
12+
* it'll be null-padded to 192-bits and 192 bits will be the key length until {@link Crypt_AES::setKey() setKey()}
13+
* is called, again, at which point, it'll be recalculated.
14+
*
15+
* Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't
16+
* make a whole lot of sense. {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance. Calling that function,
17+
* however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
18+
*
19+
* Here's a short example of how to use this library:
20+
* <code>
21+
* <?php
22+
* include 'Crypt/AES.php';
23+
*
24+
* $aes = new Crypt_AES();
25+
*
26+
* $aes->setKey('abcdefghijklmnop');
27+
*
28+
* $size = 10 * 1024;
29+
* $plaintext = '';
30+
* for ($i = 0; $i < $size; $i++) {
31+
* $plaintext.= 'a';
32+
* }
33+
*
34+
* echo $aes->decrypt($aes->encrypt($plaintext));
35+
* ?>
36+
* </code>
37+
*
38+
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
39+
* of this software and associated documentation files (the "Software"), to deal
40+
* in the Software without restriction, including without limitation the rights
41+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42+
* copies of the Software, and to permit persons to whom the Software is
43+
* furnished to do so, subject to the following conditions:
44+
*
45+
* The above copyright notice and this permission notice shall be included in
46+
* all copies or substantial portions of the Software.
47+
*
48+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
53+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
54+
* THE SOFTWARE.
55+
*
56+
* @category Crypt
57+
* @package Crypt_AES
58+
* @author Jim Wigginton <terrafrost@php.net>
59+
* @copyright MMVIII Jim Wigginton
60+
* @license http://www.opensource.org/licenses/mit-license.html MIT License
61+
* @link http://phpseclib.sourceforge.net
62+
*/
63+
64+
/**
65+
* Include Crypt_Rijndael
66+
*/
67+
if (!class_exists('Crypt_Rijndael')) {
68+
include_once 'Rijndael.php';
69+
}
70+
71+
/**#@+
72+
* @access public
73+
* @see Crypt_AES::encrypt()
74+
* @see Crypt_AES::decrypt()
75+
*/
76+
/**
77+
* Encrypt / decrypt using the Counter mode.
78+
*
79+
* Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
80+
*
81+
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
82+
*/
83+
define('CRYPT_AES_MODE_CTR', CRYPT_MODE_CTR);
84+
/**
85+
* Encrypt / decrypt using the Electronic Code Book mode.
86+
*
87+
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
88+
*/
89+
define('CRYPT_AES_MODE_ECB', CRYPT_MODE_ECB);
90+
/**
91+
* Encrypt / decrypt using the Code Book Chaining mode.
92+
*
93+
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
94+
*/
95+
define('CRYPT_AES_MODE_CBC', CRYPT_MODE_CBC);
96+
/**
97+
* Encrypt / decrypt using the Cipher Feedback mode.
98+
*
99+
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
100+
*/
101+
define('CRYPT_AES_MODE_CFB', CRYPT_MODE_CFB);
102+
/**
103+
* Encrypt / decrypt using the Cipher Feedback mode.
104+
*
105+
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
106+
*/
107+
define('CRYPT_AES_MODE_OFB', CRYPT_MODE_OFB);
108+
/**#@-*/
109+
110+
/**#@+
111+
* @access private
112+
* @see Crypt_Base::Crypt_Base()
113+
*/
114+
/**
115+
* Toggles the internal implementation
116+
*/
117+
define('CRYPT_AES_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
118+
/**
119+
* Toggles the mcrypt implementation
120+
*/
121+
define('CRYPT_AES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
122+
/**#@-*/
123+
124+
/**
125+
* Pure-PHP implementation of AES.
126+
*
127+
* @package Crypt_AES
128+
* @author Jim Wigginton <terrafrost@php.net>
129+
* @access public
130+
*/
131+
class Crypt_AES extends Crypt_Rijndael
132+
{
133+
/**
134+
* The namespace used by the cipher for its constants.
135+
*
136+
* @see Crypt_Base::const_namespace
137+
* @var String
138+
* @access private
139+
*/
140+
var $const_namespace = 'AES';
141+
142+
/**
143+
* Dummy function
144+
*
145+
* Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
146+
*
147+
* @see Crypt_Rijndael::setBlockLength()
148+
* @access public
149+
* @param Integer $length
150+
*/
151+
function setBlockLength($length)
152+
{
153+
return;
154+
}
155+
156+
/**
157+
* Sets the key length
158+
*
159+
* Valid key lengths are 128, 192, and 256. If the length is less than 128, it will be rounded up to
160+
* 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount.
161+
*
162+
* @see Crypt_Rijndael:setKeyLength()
163+
* @access public
164+
* @param Integer $length
165+
*/
166+
function setKeyLength($length)
167+
{
168+
switch ($length) {
169+
case 160:
170+
$length = 192;
171+
break;
172+
case 224:
173+
$length = 256;
174+
}
175+
parent::setKeyLength($length);
176+
}
177+
178+
/**
179+
* Sets the key.
180+
*
181+
* Rijndael supports five different key lengths, AES only supports three.
182+
*
183+
* @see Crypt_Rijndael:setKey()
184+
* @see setKeyLength()
185+
* @access public
186+
* @param String $key
187+
*/
188+
function setKey($key)
189+
{
190+
parent::setKey($key);
191+
192+
if (!$this->explicit_key_length) {
193+
$length = strlen($key);
194+
switch (true) {
195+
case $length <= 16:
196+
$this->key_size = 16;
197+
break;
198+
case $length <= 24:
199+
$this->key_size = 24;
200+
break;
201+
default:
202+
$this->key_size = 32;
203+
}
204+
$this->_setupEngine();
205+
}
206+
}
207+
}

0 commit comments

Comments
 (0)