AES.php 6.2 KB

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