此PHP代码出现一些错误。我想加密该上传文件并保存。然后在下载时,它必须解密并下载原始文件。
$handle = fopen($file, "rb") or die("could not open the file");
$contents = fread($handle, filesize($file));
fclose($handle);
$iv = substr(md5("\x18\x3C\x58" . $passphrase, true), 0, 8);
$key = substr(md5("\x2D\xFC\xD8" . $passphrase, true) . md5("\x2D\xFC\xD8" . $passphrase, true), 0, 24);
$opts = array('iv' => $iv, 'key' => $key);
$fp = fopen($destination, 'wb') or die("Could not opn file for writing");
stream_filter_append($fp, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts);
fwrite($fp, $contents) or die('Could not write to file');
fclose($fp);
}
function decrypt_file($file, $passphrase) {
$iv = substr(md5("\x18\x3C\x58" . $passphrase, true), 0, 8);
$key = substr(md5("\x2D\xFC\xD8" . $passphrase, true) . md5("\x2D\xFC\xD8" . $passphrase, true), 0, 24);
$opts = array('iv' => $iv, 'key' => $key);
$fp = fopen($file, 'rb');
stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_READ, $opts);
return $fp;
}
$filename = "test.txt";
//encrypt file
encrypt_file($filename, "encrypted/" . $filename, 'secret-password');
//decrypt file
$decrypted = decrypt_file('encrypted/' . $filename, 'secret-password');
header('Content-type:application/txt');
fpassthru($decrypted);