Encrypter.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php namespace Illuminate\Encryption;
  2. class DecryptException extends \RuntimeException {}
  3. class Encrypter {
  4. /**
  5. * The encryption key.
  6. *
  7. * @var string
  8. */
  9. protected $key;
  10. /**
  11. * The algorithm used for encryption.
  12. *
  13. * @var string
  14. */
  15. protected $cipher = 'rijndael-256';
  16. /**
  17. * The mode used for encryption.
  18. *
  19. * @var string
  20. */
  21. protected $mode = 'cbc';
  22. /**
  23. * The block size of the cipher.
  24. *
  25. * @var int
  26. */
  27. protected $block = 32;
  28. /**
  29. * Create a new encrypter instance.
  30. *
  31. * @param string $key
  32. * @return void
  33. */
  34. public function __construct($key)
  35. {
  36. $this->key = $key;
  37. }
  38. /**
  39. * Encrypt the given value.
  40. *
  41. * @param string $value
  42. * @return string
  43. */
  44. public function encrypt($value)
  45. {
  46. $iv = @mcrypt_create_iv($this->getIvSize(), $this->getRandomizer());
  47. $value = base64_encode($this->padAndMcrypt($value, $iv));
  48. // Once we have the encrypted value we will go ahead base64_encode the input
  49. // vector and create the MAC for the encrypted value so we can verify its
  50. // authenticity. Then, we'll JSON encode the data in a "payload" array.
  51. $mac = $this->hash($iv = base64_encode($iv), $value);
  52. return base64_encode(json_encode(compact('iv', 'value', 'mac')));
  53. }
  54. /**
  55. * Pad and use mcrypt on the given value and input vector.
  56. *
  57. * @param string $value
  58. * @param string $iv
  59. * @return string
  60. */
  61. protected function padAndMcrypt($value, $iv)
  62. {
  63. $value = $this->addPadding(serialize($value));
  64. return @mcrypt_encrypt($this->cipher, $this->key, $value, $this->mode, $iv);
  65. }
  66. /**
  67. * Decrypt the given value.
  68. *
  69. * @param string $payload
  70. * @return string
  71. */
  72. public function decrypt($payload)
  73. {
  74. $payload = $this->getJsonPayload($payload);
  75. // We'll go ahead and remove the PKCS7 padding from the encrypted value before
  76. // we decrypt it. Once we have the de-padded value, we will grab the vector
  77. // and decrypt the data, passing back the unserialized from of the value.
  78. $value = base64_decode($payload['value']);
  79. $iv = base64_decode($payload['iv']);
  80. return unserialize($this->stripPadding($this->mcryptDecrypt($value, $iv)));
  81. }
  82. /**
  83. * Run the mcrypt decryption routine for the value.
  84. *
  85. * @param string $value
  86. * @param string $iv
  87. * @return string
  88. */
  89. protected function mcryptDecrypt($value, $iv)
  90. {
  91. return @mcrypt_decrypt($this->cipher, $this->key, $value, $this->mode, $iv);
  92. }
  93. /**
  94. * Get the JSON array from the given payload.
  95. *
  96. * @param string $payload
  97. * @return array
  98. */
  99. protected function getJsonPayload($payload)
  100. {
  101. $payload = json_decode(base64_decode($payload), true);
  102. // If the payload is not valid JSON or does not have the proper keys set we will
  103. // assume it is invalid and bail out of the routine since we will not be able
  104. // to decrypt the given value. We'll also check the MAC for this encryption.
  105. if ( ! $payload or $this->invalidPayload($payload))
  106. {
  107. throw new DecryptException("Invalid data.");
  108. }
  109. if ( ! $this->validMac($payload))
  110. {
  111. throw new DecryptException("MAC is invalid.");
  112. }
  113. return $payload;
  114. }
  115. /**
  116. * Determine if the MAC for the given payload is valid.
  117. *
  118. * @param array $payload
  119. * @return bool
  120. */
  121. protected function validMac(array $payload)
  122. {
  123. return ($payload['mac'] == $this->hash($payload['iv'], $payload['value']));
  124. }
  125. /**
  126. * Create a MAC for the given value.
  127. *
  128. * @param string $iv
  129. * @param string $value
  130. * @return string
  131. */
  132. protected function hash($iv, $value)
  133. {
  134. return hash_hmac('sha256', $iv.$value, $this->key);
  135. }
  136. /**
  137. * Add PKCS7 padding to a given value.
  138. *
  139. * @param string $value
  140. * @return string
  141. */
  142. protected function addPadding($value)
  143. {
  144. $pad = $this->block - (strlen($value) % $this->block);
  145. return $value.str_repeat(chr($pad), $pad);
  146. }
  147. /**
  148. * Remove the padding from the given value.
  149. *
  150. * @param string $value
  151. * @return string
  152. */
  153. protected function stripPadding($value)
  154. {
  155. $pad = ord($value[($len = strlen($value)) - 1]);
  156. return $this->paddingIsValid($pad, $value) ? substr($value, 0, strlen($value) - $pad) : $value;
  157. }
  158. /**
  159. * Determine if the given padding for a value is valid.
  160. *
  161. * @param string $pad
  162. * @param string $value
  163. * @return bool
  164. */
  165. protected function paddingIsValid($pad, $value)
  166. {
  167. $beforePad = strlen($value) - $pad;
  168. return substr($value, $beforePad) == str_repeat(substr($value, -1), $pad);
  169. }
  170. /**
  171. * Verify that the encryption payload is valid.
  172. *
  173. * @param array $data
  174. * @return bool
  175. */
  176. protected function invalidPayload(array $data)
  177. {
  178. return ! isset($data['iv']) or ! isset($data['value']) or ! isset($data['mac']);
  179. }
  180. /**
  181. * Get the IV size for the cipher.
  182. *
  183. * @return int
  184. */
  185. protected function getIvSize()
  186. {
  187. return @mcrypt_get_iv_size($this->cipher, $this->mode);
  188. }
  189. /**
  190. * Get the random data source available for the OS.
  191. *
  192. * @return int
  193. */
  194. protected function getRandomizer()
  195. {
  196. if (defined('MCRYPT_DEV_URANDOM')) return MCRYPT_DEV_URANDOM;
  197. if (defined('MCRYPT_DEV_RANDOM')) return MCRYPT_DEV_RANDOM;
  198. mt_srand();
  199. return MCRYPT_RAND;
  200. }
  201. /**
  202. * Set the encryption key.
  203. *
  204. * @param string $key
  205. * @return void
  206. */
  207. public function setKey($key)
  208. {
  209. $this->key = $key;
  210. }
  211. /**
  212. * Set the encryption cipher.
  213. *
  214. * @param string $cipher
  215. * @return void
  216. */
  217. public function setCipher($cipher)
  218. {
  219. $this->cipher = $cipher;
  220. }
  221. /**
  222. * Set the encryption mode.
  223. *
  224. * @param string $mode
  225. * @return void
  226. */
  227. public function setMode($mode)
  228. {
  229. $this->mode = $mode;
  230. }
  231. }