How does mcrypt treat AES encryption differently compared to OpenSSL in PHP?
mcrypt and OpenSSL handle AES encryption differently in PHP. mcrypt has been deprecated in PHP 7.1 and removed in PHP 7.2, so it is recommended to use OpenSSL for AES encryption. To switch to OpenSSL, you can use the openssl_encrypt and openssl_decrypt functions instead of the mcrypt functions.
// Encrypt using OpenSSL
function encryptData($data, $key) {
$cipher = "aes-256-cbc";
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$encrypted = openssl_encrypt($data, $cipher, $key, 0, $iv);
return base64_encode($iv . $encrypted);
}
// Decrypt using OpenSSL
function decryptData($data, $key) {
$cipher = "aes-256-cbc";
$ivlen = openssl_cipher_iv_length($cipher);
$data = base64_decode($data);
$iv = substr($data, 0, $ivlen);
$encrypted = substr($data, $ivlen);
return openssl_decrypt($encrypted, $cipher, $key, 0, $iv);
}
Keywords
Related Questions
- What are some potential challenges in unzipping downloaded files and saving them in the correct location on the local machine using PHP?
- Which CMS platforms are known for easy integration with existing PHP solutions?
- What are the best practices for setting up and configuring a local mail server for PHP development purposes?