AES.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  50. * Pure-PHP implementation of AES.
  51. *
  52. * @package AES
  53. * @author Jim Wigginton <terrafrost@php.net>
  54. * @access public
  55. */
  56. class AES extends Rijndael
  57. {
  58. /**
  59. * Dummy function
  60. *
  61. * Since \phpseclib\Crypt\AES extends \phpseclib\Crypt\Rijndael, this function is, technically, available, but it doesn't do anything.
  62. *
  63. * @see \phpseclib\Crypt\Rijndael::setBlockLength()
  64. * @access public
  65. * @param int $length
  66. * @throws \BadMethodCallException anytime it's called
  67. */
  68. function setBlockLength($length)
  69. {
  70. throw new \BadMethodCallException('The block length cannot be set for AES.');
  71. }
  72. /**
  73. * Sets the key length
  74. *
  75. * Valid key lengths are 128, 192, and 256. Set the link to bool(false) to disable a fixed key length
  76. *
  77. * @see \phpseclib\Crypt\Rijndael:setKeyLength()
  78. * @access public
  79. * @param int $length
  80. * @throws \LengthException if the key length isn't supported
  81. */
  82. function setKeyLength($length)
  83. {
  84. switch ($length) {
  85. case 128:
  86. case 192:
  87. case 256:
  88. break;
  89. default:
  90. throw new \LengthException('Key of size ' . $length . ' not supported by this algorithm. Only keys of sizes 128, 192 or 256 supported');
  91. }
  92. parent::setKeyLength($length);
  93. }
  94. /**
  95. * Sets the key.
  96. *
  97. * Rijndael supports five different key lengths, AES only supports three.
  98. *
  99. * @see \phpseclib\Crypt\Rijndael:setKey()
  100. * @see setKeyLength()
  101. * @access public
  102. * @param string $key
  103. * @throws \LengthException if the key length isn't supported
  104. */
  105. function setKey($key)
  106. {
  107. switch (strlen($key)) {
  108. case 16:
  109. case 24:
  110. case 32:
  111. break;
  112. default:
  113. throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported');
  114. }
  115. parent::setKey($key);
  116. }
  117. }