You are here

public function Mcrypt::encrypt in AES encryption 8.2

Reverse the string.

Overrides AESPluginBase::encrypt

File

src/Plugin/AES/Mcrypt.php, line 41

Class

Mcrypt
Mcrypt plugin implementation

Namespace

Drupal\aes\Plugin\AES

Code

public function encrypt($data, $key = FALSE, $cipher = FALSE) {
  $config = FileStorageFactory::getActive()
    ->read('aes.settings');
  $iv = base64_decode($config['mcrypt_iv']);
  if (!$key) {
    $key = $config['key'];
  }
  if (!$cipher) {
    $cipher = $config['cipher'];
  }
  $td = mcrypt_module_open($cipher, '', MCRYPT_MODE_CBC, '');
  if (empty($iv)) {
    self::make_iv();
    $config = FileStorageFactory::getActive()
      ->read('aes.settings');
    $iv = base64_decode($config['mcrypt_iv']);
    \Drupal::logger('aes')
      ->warning('No initialization vector found while trying to encrypt! Recreated a new one now and will try to carry on as normal.');
  }
  $ks = mcrypt_enc_get_key_size($td);
  $key = substr(sha1($key), 0, $ks);
  mcrypt_generic_init($td, $key, $iv);
  $encrypted = mcrypt_generic($td, $data);
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);
  return $encrypted;
}