You are here

public static function AES::decrypt in AES encryption 8.2

Decrypts a string of encrypted data.

Parameters

string $string: The string to decrypt.

bool $base64encoded: Whether this encrypted string is base64 encoded or not.

string $custom_key: Use this as the key rather than the stored one for this operation.

string $custom_cipher: Use this cipher rather than the default one. (only with Mcrypt - ignored with phpseclib)

string $custom_iv: Use this initialization vector instead of the default one.

string $force_implementation: Can be 'phpseclib', 'mcrypt' or classname of custom implementation. Warning: Does not check if the requested implementation actually exists.

Return value

bool|string The decrypted string on success, false on error.

1 call to AES::decrypt()
AesTests::testAesEncryption in src/Tests/AesTests.php
Encrypt a string and decrypt it back.

File

src/AES.php, line 246

Class

AES

Namespace

Drupal\aes

Code

public static function decrypt($string, $base64encoded = TRUE, $custom_key = NULL, $custom_cipher = NULL, $custom_iv = NULL, $force_implementation = NULL) {

  // Bail out if the passed string is empty.
  if (empty($string)) {
    \Drupal::logger('aes')
      ->warning('Tried to decrypt an empty string.');
    return FALSE;
  }
  $config = FileStorageFactory::getActive()
    ->read('aes.settings');
  if ($base64encoded) {
    $string = base64_decode($string);
  }
  $cipher = empty($custom_cipher) ? $config['cipher'] : $custom_cipher;
  $key = empty($custom_key) ? self::get_key() : $custom_key;
  $implementation = $force_implementation ? $force_implementation : $config['implementation'];
  if ($implementation == 'phpseclib') {

    // The phpseclib doesn't support custom ciphers and iv's.
    if (!empty($custom_cipher)) {
      \Drupal::logger('aes')
        ->warning("A custom cipher was defined when decrypting a string in the AES module using the phpseclib implementation. This implementation doesn't support custom ciphers therefore the argument was ignored and the decryption was done with the standard cipher.");
    }
    if (!empty($custom_iv)) {
      \Drupal::logger('aes')
        ->warning("A custom IV was defined when decrypting a string in the AES module using the phpseclib implementation. This implementation doesn't support custom IV's therefore the argument was ignored and the decryption was done with the standard IV.");
    }
    if (!self::load_phpsec()) {
      return FALSE;
    }
    $phpsec = new \Crypt_AES();
    $phpsec
      ->setKey($key);
    $decrypted = $phpsec
      ->decrypt($string);
    return trim($decrypted);
  }
  if ($implementation == 'mcrypt') {

    // @todo remove this because we have Mcrypt plugin.
    $td = mcrypt_module_open($cipher, '', MCRYPT_MODE_CBC, '');
    $ks = mcrypt_enc_get_key_size($td);
    $iv = base64_decode($custom_iv ? $custom_iv : $config['mcrypt_iv']);
    if (empty($iv)) {
      \Drupal::logger('aes')
        ->error('No initialization vector found while trying to decrypt with mcrypt. Aborting!');
      return FALSE;
    }
    $key = substr(sha1($key), 0, $ks);
    mcrypt_generic_init($td, $key, $iv);
    $decrypted = mdecrypt_generic($td, $string);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return trim($decrypted);
  }

  /* @var \Drupal\aes\Plugin\AESPluginManager $plugin_manager */
  $plugin_manager = \Drupal::service('plugin.manager.aes');
  try {

    /* @var \Drupal\aes\Plugin\AESPluginBase $custom */
    $custom = $plugin_manager
      ->getInstanceById($implementation);
    $decrypted = $custom
      ->decrypt($string, $key, $cipher);
  } catch (\Exception $e) {
    $error_msg = t('AES having problems with custom plugin implementation: %plugin . Message: %msg', array(
      '%plugin' => $implementation,
      '%msg' => $e
        ->getMessage(),
    ));
    \Drupal::logger('aes')
      ->error($error_msg);
    return FALSE;
  }
  return $decrypted;
}