是否可以使用php-openssl加密图像文件?

问题描述 投票:1回答:1

我正在制作一个可以上传和下载各种文件的网络服务器。(服务器操作系统是Windows)当文件上传和下载时,它使用AES加密和解密。我曾经搜过它,我已经成功了。但它仅适用于.pdf,.csv,.txt,.pptx文件。

当我尝试打开解密的.jpg,.png,.zip,.xlsx文件时,它无法正常打开。我看不到图像。

// 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;

我不知道为什么它在图像文件中不起作用。我的代码有问题吗?

php encryption php-openssl
1个回答
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.