What are the deprecated methods to avoid when encrypting and decrypting files in PHP?
Deprecated methods to avoid when encrypting and decrypting files in PHP include using the `mcrypt_encrypt` and `mcrypt_decrypt` functions. These functions have been deprecated in favor of the `openssl_encrypt` and `openssl_decrypt` functions, which provide better security and support for modern encryption algorithms. It is recommended to use the `openssl_encrypt` and `openssl_decrypt` functions for encrypting and decrypting files in PHP.
// Encrypt file using openssl_encrypt
function encryptFile($inputFile, $outputFile, $key) {
$inputData = file_get_contents($inputFile);
$encryptedData = openssl_encrypt($inputData, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, random_bytes(16));
file_put_contents($outputFile, $encryptedData);
}
// Decrypt file using openssl_decrypt
function decryptFile($inputFile, $outputFile, $key) {
$encryptedData = file_get_contents($inputFile);
$decryptedData = openssl_decrypt($encryptedData, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, random_bytes(16));
file_put_contents($outputFile, $decryptedData);
}
// Example usage
$key = 'secretkey';
encryptFile('plaintext.txt', 'encrypted.txt', $key);
decryptFile('encrypted.txt', 'decrypted.txt', $key);