public static function AES::encrypt in AES encryption 8.2
Encrypts a string.
Parameters
string $string: The string to encrypt.
bool $base64encode: Whether to return the string base64 encoded (recommended for database insertion).
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 encrypted string on success, false on error.
1 call to AES::encrypt()
- AesTests::testAesEncryption in src/
Tests/ AesTests.php - Encrypt a string and decrypt it back.
File
- src/
AES.php, line 159
Class
Namespace
Drupal\aesCode
public static function encrypt($string, $base64encode = 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 encrypt an empty string.');
return FALSE;
}
$config = FileStorageFactory::getActive()
->read('aes.settings');
$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 encrypting a string in the AES module using the phpseclib implementation. This implementation doesn't support custom ciphers therefore the argument was ignored and the encryption was done with the standard cipher.");
}
if (!empty($custom_iv)) {
\Drupal::logger('aes')
->warning("A custom IV was defined when encrypting 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 encryption was done with the standard IV.");
}
if (!self::load_phpsec()) {
return FALSE;
}
$phpsec = new \Crypt_AES();
$phpsec
->setKey($key);
$encrypted = $phpsec
->encrypt($string);
return $base64encode ? base64_encode($encrypted) : $encrypted;
}
if ($implementation == 'mcrypt') {
// @todo remove this because we have Mcrypt plugin.
$td = mcrypt_module_open($cipher, '', MCRYPT_MODE_CBC, '');
$iv = base64_decode($custom_iv ? $custom_iv : $config['mcrypt_iv']);
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, $string);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $base64encode ? base64_encode($encrypted) : $encrypted;
}
/* @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);
$encrypted = $custom
->encrypt($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 $base64encode ? base64_encode($encrypted) : $encrypted;
}