AES.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Pure-PHP implementation of AES.
  4. *
  5. * Uses mcrypt, if available/possible, and an internal implementation, otherwise.
  6. *
  7. * PHP version 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 \phpseclib\Crypt\AES extends \phpseclib\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 'vendor/autoload.php';
  26. *
  27. * $aes = new \phpseclib\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. * @category Crypt
  42. * @package AES
  43. * @author Jim Wigginton <terrafrost@php.net>
  44. * @copyright 2008 Jim Wigginton
  45. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  46. * @link http://phpseclib.sourceforge.net
  47. */
  48. //namespace phpseclib\Crypt;
  49. include PHPAES_ROOT . '/phpseclib/Crypt/Rijndael.php';
  50. /**
  51. * Pure-PHP implementation of AES.
  52. *
  53. * @package AES
  54. * @author Jim Wigginton <terrafrost@php.net>
  55. * @access public
  56. */
  57. class AES extends Rijndael
  58. {
  59. /**
  60. * Dummy function
  61. *
  62. * Since \phpseclib\Crypt\AES extends \phpseclib\Crypt\Rijndael, this function is, technically, available, but it doesn't do anything.
  63. *
  64. * @see \phpseclib\Crypt\Rijndael::setBlockLength()
  65. * @access public
  66. * @param int $length
  67. * @throws \BadMethodCallException anytime it's called
  68. */
  69. public function setBlockLength($length)
  70. {
  71. throw new \BadMethodCallException('The block length cannot be set for AES.');
  72. }
  73. /**
  74. * Sets the key length
  75. *
  76. * Valid key lengths are 128, 192, and 256. Set the link to bool(false) to disable a fixed key length
  77. *
  78. * @see \phpseclib\Crypt\Rijndael:setKeyLength()
  79. * @access public
  80. * @param int $length
  81. * @throws \LengthException if the key length isn't supported
  82. */
  83. public function setKeyLength($length)
  84. {
  85. switch ($length) {
  86. case 128:
  87. case 192:
  88. case 256:
  89. break;
  90. default:
  91. throw new \LengthException('Key of size ' . $length . ' not supported by this algorithm. Only keys of sizes 128, 192 or 256 supported');
  92. }
  93. parent::setKeyLength($length);
  94. }
  95. /**
  96. * Sets the key.
  97. *
  98. * Rijndael supports five different key lengths, AES only supports three.
  99. *
  100. * @see \phpseclib\Crypt\Rijndael:setKey()
  101. * @see setKeyLength()
  102. * @access public
  103. * @param string $key
  104. * @throws \LengthException if the key length isn't supported
  105. */
  106. public function setKey($key)
  107. {
  108. switch (strlen($key)) {
  109. case 16:
  110. case 24:
  111. case 32:
  112. break;
  113. default:
  114. throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported');
  115. }
  116. parent::setKey($key);
  117. }
  118. /**
  119. * Returns the class that defines the private methods
  120. *
  121. * @access private
  122. * @return string
  123. */
  124. protected function getClassContext()
  125. {
  126. return 'phpseclib\Crypt\Rijndael';
  127. }
  128. }