You are here

function encrypt_encrypt_mcrypt_rij_256 in Encrypt 6

Same name and namespace in other branches
  1. 7 includes/encrypt.encrypt.inc \encrypt_encrypt_mcrypt_rij_256()

Call back for Encrypt implementation: Mcrypt

This method uses a PHPs mcrypt extension and AES-256

1 string reference to 'encrypt_encrypt_mcrypt_rij_256'
encrypt_encrypt_method_info in includes/encrypt.encrypt.inc
Implementation of encrypt_method_info().

File

includes/encrypt.encrypt.inc, line 94
This file holds the hook implementation for the encrypt modules

Code

function encrypt_encrypt_mcrypt_rij_256($op = 'encrypt', $text = '', $key = ENCRYPT_DEFAULT_KEY_NONE, $options = array()) {
  $processed_text = '';
  $options = is_array($options) ? $options : array();

  // Key cannot be too long for this encryption
  $key = drupal_substr($key, 0, 32);

  // Check base64 flag
  $options['base64'] = isset($options['base64']) ? $options['base64'] : TRUE;

  // Define iv cipher
  $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

  // Check op
  if ($op == 'decrypt') {

    // Decrypt
    // Check if we are using base64 encoding
    if ($options['base64'] !== FALSE) {
      $text = base64_decode($text);
    }

    // Decrypt text
    $processed_text = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
  }
  else {

    // Encrypt
    $processed_text = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);

    // Check if we are using base64 encoding
    if ($options['base64'] !== FALSE) {
      $processed_text = base64_encode($processed_text);
    }
  }
  return trim($processed_text);
}