/libraries/fof/encrypt/aes.php

https://github.com/dextercowley/joomla-cms · PHP · 239 lines · 144 code · 40 blank · 55 comment · 24 complexity · 4b355827fe24bb4349536b022f27927f MD5 · raw file

  1. <?php
  2. /**
  3. * @package FrameworkOnFramework
  4. * @subpackage encrypt
  5. * @copyright Copyright (C) 2010 - 2014 Akeeba Ltd. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. // Protect from unauthorized access
  9. defined('FOF_INCLUDED') or die;
  10. /**
  11. * A simple implementation of AES-128, AES-192 and AES-256 encryption using the
  12. * high performance mcrypt library.
  13. *
  14. * @package FrameworkOnFramework
  15. * @since 1.0
  16. */
  17. class FOFEncryptAes
  18. {
  19. /** @var string The AES cipher to use (this is an mcrypt identifier, not the bit strength) */
  20. private $_cipherType = 0;
  21. /** @var string Cipher mode. Can be CBC or ECB. We recommend using CBC */
  22. private $_cipherMode = 0;
  23. /** @var string The cipher key (password) */
  24. private $_keyString = '';
  25. /**
  26. * Initialise the AES encryption object
  27. *
  28. * @param string $key The encryption key (password). It can be a raw key (32 bytes) or a passphrase.
  29. * @param int $strength Bit strength (128, 192 or 256)
  30. * @param string $mode Ecnryption mode. Can be ebc or cbc. We recommend using cbc.
  31. */
  32. public function __construct($key, $strength = 256, $mode = 'cbc')
  33. {
  34. $this->_keyString = $key;
  35. switch ($strength)
  36. {
  37. case 256:
  38. default:
  39. $this->_cipherType = MCRYPT_RIJNDAEL_256;
  40. break;
  41. case 192:
  42. $this->_cipherType = MCRYPT_RIJNDAEL_192;
  43. break;
  44. case 128:
  45. $this->_cipherType = MCRYPT_RIJNDAEL_128;
  46. break;
  47. }
  48. switch (strtoupper($mode))
  49. {
  50. case 'ECB':
  51. $this->_cipherMode = MCRYPT_MODE_ECB;
  52. break;
  53. case 'CBC':
  54. $this->_cipherMode = MCRYPT_MODE_CBC;
  55. break;
  56. }
  57. }
  58. /**
  59. * Encrypts a string using AES
  60. *
  61. * @param string $stringToEncrypt The plaintext to encrypt
  62. * @param bool $base64encoded Should I Base64-encode the result?
  63. *
  64. * @return string The cryptotext. Please note that the first 16 bytes of
  65. * the raw string is the IV (initialisation vector) which
  66. * is necessary for decoding the string.
  67. */
  68. public function encryptString($stringToEncrypt, $base64encoded = true)
  69. {
  70. if (strlen($this->_keyString) != 32)
  71. {
  72. $key = hash('sha256', $this->_keyString, true);
  73. }
  74. else
  75. {
  76. $key = $this->_keyString;
  77. }
  78. // Set up the IV (Initialization Vector)
  79. $iv_size = mcrypt_get_iv_size($this->_cipherType, $this->_cipherMode);
  80. $iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);
  81. if (empty($iv))
  82. {
  83. $iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_RANDOM);
  84. }
  85. if (empty($iv))
  86. {
  87. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  88. }
  89. // Encrypt the data
  90. $cipherText = mcrypt_encrypt($this->_cipherType, $key, $stringToEncrypt, $this->_cipherMode, $iv);
  91. // Prepend the IV to the ciphertext
  92. $cipherText = $iv . $cipherText;
  93. // Optionally pass the result through Base64 encoding
  94. if ($base64encoded)
  95. {
  96. $cipherText = base64_encode($cipherText);
  97. }
  98. // Return the result
  99. return $cipherText;
  100. }
  101. /**
  102. * Decrypts a ciphertext into a plaintext string using AES
  103. *
  104. * @param string $stringToDecrypt The ciphertext to decrypt. The first 16 bytes of the raw string must contain the IV (initialisation vector).
  105. * @param bool $base64encoded Should I Base64-decode the data before decryption?
  106. *
  107. * @return string The plain text string
  108. */
  109. public function decryptString($stringToDecrypt, $base64encoded = true)
  110. {
  111. if (strlen($this->_keyString) != 32)
  112. {
  113. $key = hash('sha256', $this->_keyString, true);
  114. }
  115. else
  116. {
  117. $key = $this->_keyString;
  118. }
  119. if ($base64encoded)
  120. {
  121. $stringToDecrypt = base64_decode($stringToDecrypt);
  122. }
  123. // Calculate the IV size
  124. $iv_size = mcrypt_get_iv_size($this->_cipherType, $this->_cipherMode);
  125. // Extract IV
  126. $iv = substr($stringToDecrypt, 0, $iv_size);
  127. $stringToDecrypt = substr($stringToDecrypt, $iv_size);
  128. // Decrypt the data
  129. $plainText = mcrypt_decrypt($this->_cipherType, $key, $stringToDecrypt, $this->_cipherMode, $iv);
  130. return $plainText;
  131. }
  132. /**
  133. * Is AES encryption supported by this PHP installation?
  134. *
  135. * @return boolean
  136. */
  137. public static function isSupported()
  138. {
  139. if (!function_exists('mcrypt_get_key_size'))
  140. {
  141. return false;
  142. }
  143. if (!function_exists('mcrypt_get_iv_size'))
  144. {
  145. return false;
  146. }
  147. if (!function_exists('mcrypt_create_iv'))
  148. {
  149. return false;
  150. }
  151. if (!function_exists('mcrypt_encrypt'))
  152. {
  153. return false;
  154. }
  155. if (!function_exists('mcrypt_decrypt'))
  156. {
  157. return false;
  158. }
  159. if (!function_exists('mcrypt_list_algorithms'))
  160. {
  161. return false;
  162. }
  163. if (!function_exists('hash'))
  164. {
  165. return false;
  166. }
  167. if (!function_exists('hash_algos'))
  168. {
  169. return false;
  170. }
  171. if (!function_exists('base64_encode'))
  172. {
  173. return false;
  174. }
  175. if (!function_exists('base64_decode'))
  176. {
  177. return false;
  178. }
  179. $algorightms = mcrypt_list_algorithms();
  180. if (!in_array('rijndael-128', $algorightms))
  181. {
  182. return false;
  183. }
  184. if (!in_array('rijndael-192', $algorightms))
  185. {
  186. return false;
  187. }
  188. if (!in_array('rijndael-256', $algorightms))
  189. {
  190. return false;
  191. }
  192. $algorightms = hash_algos();
  193. if (!in_array('sha256', $algorightms))
  194. {
  195. return false;
  196. }
  197. return true;
  198. }
  199. }