crypt.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php namespace System;
  2. class Crypt {
  3. /**
  4. * The encryption cipher.
  5. *
  6. * @var string
  7. */
  8. public static $cipher = 'rijndael-256';
  9. /**
  10. * The encryption mode.
  11. *
  12. * @var string
  13. */
  14. public static $mode = 'cbc';
  15. /**
  16. * Encrypt a value using the MCrypt library.
  17. *
  18. * @param string $value
  19. * @return string
  20. */
  21. public static function encrypt($value)
  22. {
  23. // -----------------------------------------------------
  24. // Determine the input vector source.
  25. // -----------------------------------------------------
  26. if (defined('MCRYPT_DEV_URANDOM'))
  27. {
  28. $random = MCRYPT_DEV_URANDOM;
  29. }
  30. elseif (defined('MCRYPT_DEV_RANDOM'))
  31. {
  32. $random = MCRYPT_DEV_RANDOM;
  33. }
  34. else
  35. {
  36. $random = MCRYPT_RAND;
  37. }
  38. // -----------------------------------------------------
  39. // The system random number generator must be seeded.
  40. // -----------------------------------------------------
  41. if ($random === MCRYPT_RAND)
  42. {
  43. mt_srand();
  44. }
  45. $iv = mcrypt_create_iv(static::iv_size(), $random);
  46. $value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
  47. // -----------------------------------------------------
  48. // Use base64 encoding to get a string value.
  49. // -----------------------------------------------------
  50. return base64_encode($iv.$value);
  51. }
  52. /**
  53. * Decrypt a value using the MCrypt library.
  54. *
  55. * @param string $value
  56. * @return string
  57. */
  58. public static function decrypt($value)
  59. {
  60. $value = base64_decode($value, true);
  61. if ( ! $value)
  62. {
  63. throw new \Exception('Decryption error. Input value is not valid base64 data.');
  64. }
  65. // -----------------------------------------------------
  66. // Extract the input vector from the value.
  67. // -----------------------------------------------------
  68. $iv = substr($value, 0, static::iv_size());
  69. // -----------------------------------------------------
  70. // Remove the input vector from the value.
  71. // -----------------------------------------------------
  72. $value = substr($value, static::iv_size());
  73. return rtrim(mcrypt_decrypt(static::$cipher, static::key(), $value, static::$mode, $iv), "\0");
  74. }
  75. /**
  76. * Get the application key.
  77. *
  78. * @return string
  79. */
  80. private static function key()
  81. {
  82. if (is_null($key = Config::get('application.key')) or $key == '')
  83. {
  84. throw new \Exception("The encryption class can not be used without an encryption key.");
  85. }
  86. return $key;
  87. }
  88. /**
  89. * Get the input vector size for the cipher and mode.
  90. *
  91. * @return int
  92. */
  93. private static function iv_size()
  94. {
  95. return mcrypt_get_iv_size(static::$cipher, static::$mode);
  96. }
  97. }