What are best practices for transitioning from PHP4 to PHP5 in terms of encryption techniques?

When transitioning from PHP4 to PHP5, it is important to update encryption techniques to ensure security and compatibility with newer versions of PHP. One recommended best practice is to switch from using the outdated mcrypt extension to the more modern OpenSSL extension for encryption.

// Encrypt data using OpenSSL in PHP5
function encryptData($data, $key) {
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
    $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, 0, $iv);
    return base64_encode($iv . $encrypted);
}

// Decrypt data using OpenSSL in PHP5
function decryptData($data, $key) {
    $data = base64_decode($data);
    $iv = substr($data, 0, openssl_cipher_iv_length('aes-256-cbc'));
    $encrypted = substr($data, openssl_cipher_iv_length('aes-256-cbc'));
    return openssl_decrypt($encrypted, 'aes-256-cbc', $key, 0, $iv);
}

// Usage example
$key = 'secret_key';
$data = 'Hello, World!';
$encryptedData = encryptData($data, $key);
echo "Encrypted data: " . $encryptedData . "\n";
$decryptedData = decryptData($encryptedData, $key);
echo "Decrypted data: " . $decryptedData . "\n";