/libraries/fof/encrypt/aes.php

https://github.com/CCI-Studios/Wee-Magazine · PHP · 167 lines · 92 code · 26 blank · 49 comment · 24 complexity · 1979ed26023b0b7c97702381910f4f12 MD5 · raw file

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