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.

dbcrypt.class.php 1.1KB

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