I need to secure the user profile information, which contains predefined & custom fields such as username, profile picture & phone number.

I do need to encrypt & store it in DB. And then decrypt to display output.

Do anyone know, does encryption for fields is possible?


Yes it's possible only if you do it programatically for this. You can use hook_entity_presave() and update encrypted values while saving user. And while retrieving data/fields you can decrypt with the key that you used for encryption and show in profile view page. Example:

 * @param array $edit
 * @param type $account
 * @param type $category
function example_user_presave(&$edit, $account, $category) {
  $name = $edit['name']; // Get value of field.
  $edit['name'] = encrypt($name); // Encrypt and save.

 * Returns encrypted string
function encrypt($pure_string) {
  $encryption_key = 'SECRET';
  $encrypted_string = openssl_encrypt($pure_string, "AES-128-ECB", $encryption_key);
  return $encrypted_string;

 * Returns decrypted original string
function decrypt($encrypted_string) {
  $encryption_key = 'SECRET';
  $decrypted_string = openssl_decrypt($encrypted_string, "AES-128-ECB", $encryption_key);
  return $decrypted_string;
| improve this answer | |
  • Thanks, for decryption i need to call this function, wherever it requires as it becomes so tedious. Do we have any hook to decrypt whenever account information is fetched? – Prasanth R May 4 '18 at 9:22
  • There are no hooks for this. Write this encrypt and decrypt in any .inc file and include where necessary. – Ajay Reddy May 4 '18 at 9:23
  • Yes there’s a hook for this @Prasanth: hook_user_load – Clive May 5 '18 at 8:29

Have a look at this https://www.drupal.org/project/field_encrypt

This module adds options to encrypt field values.

The goal of this module is to create a method for encrypting field values when stored in the database.

This module depends on the Encrypt module (http://drupal.org/project/encrypt) for encrypting and decrypting strings.

Install this module like any other module. Field encryption settings are made on the field edit page.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.