function aes_encrypt in AES encryption 5
Same name and namespace in other branches
- 6 aes.module \aes_encrypt()
- 7 aes.module \aes_encrypt()
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.:
string $custom_iv Use this initialization vector instead of the default one.:
Return value
string The encrypted string.
2 calls to aes_encrypt()
- aes_config_submit in ./
aes.module - aes_user in ./
aes.module
File
- ./
aes.module, line 458
Code
function aes_encrypt($string, $base64encode = true, $custom_key = null, $custom_cipher = null, $custom_iv = null) {
if ($custom_cipher != null) {
$cipher = $custom_cipher;
}
else {
$cipher = variable_get("aes_cipher", "rijndael-128");
}
$td = mcrypt_module_open($cipher, "", MCRYPT_MODE_CBC, "");
if ($custom_iv == null) {
$iv = base64_decode(variable_get("aes_encryption_iv", ""));
}
else {
$iv = base64_decode($custom_iv);
}
if (empty($iv)) {
aes_make_iv();
$iv = base64_decode(variable_get("aes_encryption_iv", ""));
watchdog("aes", "No initialization vector found while trying to encrypt! This could be a bit of a pain since you might have to reset all the passwords for all users. I've created a new one now and will try to carry on as normal.", array(), WATCHDOG_WARNING);
}
$ks = mcrypt_enc_get_key_size($td);
if (!empty($custom_key)) {
$key = $custom_key;
}
else {
$key = aes_get_key();
}
$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);
if ($base64encode) {
return base64_encode($encrypted);
}
else {
return $encrypted;
}
}