AES.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  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. * Include Crypt_Rijndael
  65. */
  66. if (!class_exists('Crypt_Rijndael')) {
  67. require_once('Rijndael.php');
  68. }
  69. /**#@+
  70. * @access public
  71. * @see Crypt_AES::encrypt()
  72. * @see Crypt_AES::decrypt()
  73. */
  74. /**
  75. * Encrypt / decrypt using the Counter mode.
  76. *
  77. * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
  78. *
  79. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
  80. */
  81. define('CRYPT_AES_MODE_CTR', CRYPT_MODE_CTR);
  82. /**
  83. * Encrypt / decrypt using the Electronic Code Book mode.
  84. *
  85. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
  86. */
  87. define('CRYPT_AES_MODE_ECB', CRYPT_MODE_ECB);
  88. /**
  89. * Encrypt / decrypt using the Code Book Chaining mode.
  90. *
  91. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
  92. */
  93. define('CRYPT_AES_MODE_CBC', CRYPT_MODE_CBC);
  94. /**
  95. * Encrypt / decrypt using the Cipher Feedback mode.
  96. *
  97. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
  98. */
  99. define('CRYPT_AES_MODE_CFB', CRYPT_MODE_CFB);
  100. /**
  101. * Encrypt / decrypt using the Cipher Feedback mode.
  102. *
  103. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
  104. */
  105. define('CRYPT_AES_MODE_OFB', CRYPT_MODE_OFB);
  106. /**#@-*/
  107. /**#@+
  108. * @access private
  109. * @see Crypt_AES::Crypt_AES()
  110. */
  111. /**
  112. * Toggles the internal implementation
  113. */
  114. define('CRYPT_AES_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
  115. /**
  116. * Toggles the mcrypt implementation
  117. */
  118. define('CRYPT_AES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
  119. /**#@-*/
  120. /**
  121. * Pure-PHP implementation of AES.
  122. *
  123. * @author Jim Wigginton <terrafrost@php.net>
  124. * @version 0.1.0
  125. * @access public
  126. * @package Crypt_AES
  127. */
  128. class Crypt_AES extends Crypt_Rijndael {
  129. /**
  130. * The namespace used by the cipher for its constants.
  131. *
  132. * @see Crypt_Base::const_namespace
  133. * @var String
  134. * @access private
  135. */
  136. var $const_namespace = 'AES';
  137. /**
  138. * Default Constructor.
  139. *
  140. * Determines whether or not the mcrypt extension should be used.
  141. *
  142. * $mode could be:
  143. *
  144. * - CRYPT_AES_MODE_ECB
  145. *
  146. * - CRYPT_AES_MODE_CBC
  147. *
  148. * - CRYPT_AES_MODE_CTR
  149. *
  150. * - CRYPT_AES_MODE_CFB
  151. *
  152. * - CRYPT_AES_MODE_OFB
  153. *
  154. * If not explictly set, CRYPT_AES_MODE_CBC will be used.
  155. *
  156. * @see Crypt_Rijndael::Crypt_Rijndael()
  157. * @see Crypt_Base::Crypt_Base()
  158. * @param optional Integer $mode
  159. * @access public
  160. */
  161. function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
  162. {
  163. parent::Crypt_Rijndael($mode);
  164. }
  165. /**
  166. * Dummy function
  167. *
  168. * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
  169. *
  170. * @see Crypt_Rijndael::setBlockLength()
  171. * @access public
  172. * @param Integer $length
  173. */
  174. function setBlockLength($length)
  175. {
  176. return;
  177. }
  178. }
  179. // vim: ts=4:sw=4:et:
  180. // vim6: fdl=1: