OpenSsl.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. use LogicException;
  17. /**
  18. * OpenSSL implementation of crypto features for Cake\Utility\Security
  19. *
  20. * OpenSSL should be favored over mcrypt as it is actively maintained and
  21. * more widely available.
  22. *
  23. * This class is not intended to be used directly and should only
  24. * be used in the context of Cake\Utility\Security.
  25. *
  26. * @internal
  27. */
  28. class OpenSsl
  29. {
  30. /**
  31. * Not implemented
  32. *
  33. * @param string $text Encrypted string to decrypt, normal string to encrypt
  34. * @param string $key Key to use as the encryption key for encrypted data.
  35. * @param string $operation Operation to perform, encrypt or decrypt
  36. * @throws \LogicException Rijndael compatibility does not exist with Openssl.
  37. * @return void
  38. */
  39. public static function rijndael($text, $key, $operation)
  40. {
  41. throw new LogicException('rijndael is not compatible with OpenSSL. Use mcrypt instead.');
  42. }
  43. /**
  44. * Encrypt a value using AES-256.
  45. *
  46. * *Caveat* You cannot properly encrypt/decrypt data with trailing null bytes.
  47. * Any trailing null bytes will be removed on decryption due to how PHP pads messages
  48. * with nulls prior to encryption.
  49. *
  50. * @param string $plain The value to encrypt.
  51. * @param string $key The 256 bit/32 byte key to use as a cipher key.
  52. * @return string Encrypted data.
  53. * @throws \InvalidArgumentException On invalid data or key.
  54. */
  55. public static function encrypt($plain, $key)
  56. {
  57. $method = 'AES-256-CBC';
  58. $ivSize = openssl_cipher_iv_length($method);
  59. $iv = openssl_random_pseudo_bytes($ivSize);
  60. return $iv . openssl_encrypt($plain, $method, $key, OPENSSL_RAW_DATA, $iv);
  61. }
  62. /**
  63. * Decrypt a value using AES-256.
  64. *
  65. * @param string $cipher The ciphertext to decrypt.
  66. * @param string $key The 256 bit/32 byte key to use as a cipher key.
  67. * @return string Decrypted data. Any trailing null bytes will be removed.
  68. * @throws \InvalidArgumentException On invalid data or key.
  69. */
  70. public static function decrypt($cipher, $key)
  71. {
  72. $method = 'AES-256-CBC';
  73. $ivSize = openssl_cipher_iv_length($method);
  74. $iv = mb_substr($cipher, 0, $ivSize, '8bit');
  75. $cipher = mb_substr($cipher, $ivSize, null, '8bit');
  76. return openssl_decrypt($cipher, $method, $key, OPENSSL_RAW_DATA, $iv);
  77. }
  78. }