Mcrypt.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Utility\Crypto;
  16. /**
  17. * Mcrypt implementation of crypto features for Cake\Utility\Security
  18. *
  19. * This class is not intended to be used directly and should only
  20. * be used in the context of Cake\Utility\Security.
  21. *
  22. * @deprecated 3.3.0 It is recommended to use {@see Cake\Utility\Crypto\OpenSsl} instead.
  23. * @internal
  24. */
  25. class Mcrypt
  26. {
  27. /**
  28. * Encrypts/Decrypts a text using the given key using rijndael method.
  29. *
  30. * @param string $text Encrypted string to decrypt, normal string to encrypt
  31. * @param string $key Key to use as the encryption key for encrypted data.
  32. * @param string $operation Operation to perform, encrypt or decrypt
  33. * @throws \LogicException When there are errors.
  34. * @return string Encrypted binary string data, or decrypted data depending on operation.
  35. * @deprecated 3.3.0 This method will be removed in 4.0.0.
  36. */
  37. public static function rijndael($text, $key, $operation)
  38. {
  39. $algorithm = MCRYPT_RIJNDAEL_256;
  40. $mode = MCRYPT_MODE_CBC;
  41. $ivSize = mcrypt_get_iv_size($algorithm, $mode);
  42. $cryptKey = mb_substr($key, 0, 32, '8bit');
  43. if ($operation === 'encrypt') {
  44. $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_URANDOM);
  45. return $iv . '$$' . mcrypt_encrypt($algorithm, $cryptKey, $text, $mode, $iv);
  46. }
  47. $iv = mb_substr($text, 0, $ivSize, '8bit');
  48. $text = mb_substr($text, $ivSize + 2, null, '8bit');
  49. return rtrim(mcrypt_decrypt($algorithm, $cryptKey, $text, $mode, $iv), "\0");
  50. }
  51. /**
  52. * Encrypt a value using AES-256.
  53. *
  54. * *Caveat* You cannot properly encrypt/decrypt data with trailing null bytes.
  55. * Any trailing null bytes will be removed on decryption due to how PHP pads messages
  56. * with nulls prior to encryption.
  57. *
  58. * @param string $plain The value to encrypt.
  59. * @param string $key The 256 bit/32 byte key to use as a cipher key.
  60. * @return string Encrypted data.
  61. * @throws \InvalidArgumentException On invalid data or key.
  62. * @deprecated 3.3.0 Use Cake\Utility\Crypto\OpenSsl::encrypt() instead.
  63. */
  64. public static function encrypt($plain, $key)
  65. {
  66. deprecationWarning(
  67. 'Mcrypt::encrypt() is deprecated. ' .
  68. 'Use Cake\Utility\Crypto\OpenSsl::encrypt() instead.'
  69. );
  70. $algorithm = MCRYPT_RIJNDAEL_128;
  71. $mode = MCRYPT_MODE_CBC;
  72. $ivSize = mcrypt_get_iv_size($algorithm, $mode);
  73. $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_URANDOM);
  74. // Pad out plain to make it AES compatible.
  75. $pad = ($ivSize - (mb_strlen($plain, '8bit') % $ivSize));
  76. $plain .= str_repeat(chr($pad), $pad);
  77. return $iv . mcrypt_encrypt($algorithm, $key, $plain, $mode, $iv);
  78. }
  79. /**
  80. * Decrypt a value using AES-256.
  81. *
  82. * @param string $cipher The ciphertext to decrypt.
  83. * @param string $key The 256 bit/32 byte key to use as a cipher key.
  84. * @return string Decrypted data. Any trailing null bytes will be removed.
  85. * @throws \InvalidArgumentException On invalid data or key.
  86. * @deprecated 3.3.0 Use Cake\Utility\Crypto\OpenSsl::decrypt() instead.
  87. */
  88. public static function decrypt($cipher, $key)
  89. {
  90. deprecationWarning(
  91. 'Mcrypt::decrypt() is deprecated. ' .
  92. 'Use Cake\Utility\Crypto\OpenSsl::decrypt() instead.'
  93. );
  94. $algorithm = MCRYPT_RIJNDAEL_128;
  95. $mode = MCRYPT_MODE_CBC;
  96. $ivSize = mcrypt_get_iv_size($algorithm, $mode);
  97. $iv = mb_substr($cipher, 0, $ivSize, '8bit');
  98. $cipher = mb_substr($cipher, $ivSize, null, '8bit');
  99. $plain = mcrypt_decrypt($algorithm, $key, $cipher, $mode, $iv);
  100. // Remove PKCS#7 padding or Null bytes
  101. // Newer values will be PKCS#7 padded, while old
  102. // mcrypt values will be null byte padded.
  103. $padChar = mb_substr($plain, -1, null, '8bit');
  104. if ($padChar === "\0") {
  105. return trim($plain, "\0");
  106. }
  107. $padLen = ord($padChar);
  108. $result = mb_substr($plain, 0, -$padLen, '8bit');
  109. return $result === '' ? false : $result;
  110. }
  111. }