AES.php 6.1 KB

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