Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypto.class.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. class Crypto
  3. {
  4. /**
  5. * Encrypts input text for use in database
  6. *
  7. * @param string $plaintext
  8. * @return encrypted string or false if DB key not accessible
  9. */
  10. public static function encrypt($plaintext)
  11. {
  12. if (apcu_exists('DBKEY')) {
  13. $iv_size = openssl_cipher_iv_length('AES-128-CBC');
  14. $iv = openssl_random_pseudo_bytes($iv_size);
  15. $ret = base64_encode($iv.openssl_encrypt($plaintext, 'AES-128-CBC', apcu_fetch('DBKEY'), OPENSSL_RAW_DATA, $iv));
  16. return $ret;
  17. } else {
  18. return false;
  19. }
  20. }
  21. /**
  22. * Decrypts input text from database
  23. *
  24. * @param string $ciphertext
  25. * @return decrypted string string or false if DB key not accessible
  26. */
  27. public static function decrypt($ciphertext)
  28. {
  29. if (apcu_exists('DBKEY')) {
  30. $iv_size = openssl_cipher_iv_length('AES-128-CBC');
  31. $iv = substr(base64_decode($ciphertext), 0, $iv_size);
  32. $ciphertext = substr(base64_decode($ciphertext), $iv_size);
  33. return openssl_decrypt($ciphertext, 'AES-128-CBC', apcu_fetch('DBKEY'), OPENSSL_RAW_DATA, $iv);
  34. } else {
  35. return false;
  36. }
  37. }
  38. }