I'm making a web server which can upload and download all kind of file with php.(server os is windows) And when file is uploaded and downloaded, it is encrypted and decrypted using AES. I had searched about it, and I have made it. But it works only .pdf, .csv, .txt, .pptx file.
When I try to open .jpg, .png, .zip, .xlsx file which is decrypted, it doesn't open properly. I cannot see the image.
// File encryption code
$filename = $_FILES['user_file']["name"];
$key = '1234';
$iv = '12dasdq3g5b2434b';
$file = fopen('filePath', 'rb');
$fileData = fread($file, filesize('filePath'));
fclose($file);
$encryptedData = openssl_encrypt($fileData, "AES-256-CBC", $key, 0, $iv);
$fileToWrite = fopen('filePath', 'wb');
fwrite($fileToWrite, $encryptedData);
fclose($fileToWrite);
// File decryption code
$key = '1234';
$iv = '12dasdq3g5b2434b';
$file = fopen('filePath', 'rb');
$fileData = fread($file, filesize('filePath'));
fclose($file);
$decryptedData = openssl_decrypt($fileData, "AES-256-CBC", $key, 0, $iv);
header("Pragma: public");
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Expires: 0");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . $filename . "\";");
$size = strlen($decryptedData);
header("Content-Length: " . $size);
echo $decryptedData;
die;
I don't know why it doesn't work at image file. Is there any problem with my code?