This is a function that is the building block of one wordpress plugin. I came up with this code after some search but i need to optimize it and get any info from more experienced guys.
public static function afz_encrypt_decrypt( $action, $string ) {
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = '12345678901234567890123456789012';
$iv_size = 16;
if ( $action == 'encrypt' ) {
$encrypt_iv = mcrypt_create_iv( $iv_size, MCRYPT_DEV_URANDOM );
$output = openssl_encrypt( $string, $encrypt_method, $secret_key, 0, $encrypt_iv );
$output = base64_encode( $encrypt_iv . $output );
} else if ( $action == 'decrypt' ) {
$decrypt_iv = substr( base64_decode( $string ), 0, $iv_size );
$output = stripslashes( openssl_decrypt( substr( base64_decode( $string ), $iv_size ), $encrypt_method,
$secret_key,
0,
$decrypt_iv ) );
if ( false === $output ) {
$output = $string;
}
}
return $output;
}
Update: The stripslashes is indeed unneeded and i will take it out of context of this function. I need it because wordpress automatically escapes strings regardless if the relevant PHP Setting. See the NOTES section here: CODEX Link
What i still need though is more insight (if possible) to Tim's answer in regards to the security aspect of the source.