【发布时间】:2018-07-15 15:08:20
【问题描述】:
我使用这两个函数来加密/解密文件:
private function encrypt_file($source,$destination,$passphrase,$stream=NULL) {
// $source can be a local file...
if($stream) {
$contents = $source;
// OR $source can be a stream if the third argument ($stream flag) exists.
}else{
$handle = fopen($source, "rb");
$contents = @fread($handle, filesize($source));
fclose($handle);
}
$iv = substr(md5("\x1B\x3C\x58".$passphrase, true), 0, 8);
$key = substr(md5("\x2D\xFC\xD8".$passphrase, true) . md5("\x2D\xFC\xD9".$passphrase, true), 0, 24);
$opts = array('iv'=>$iv, 'key'=>$key);
$fp = fopen($destination, 'wb') or die("Could not open 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);
}
private function decrypt_file($file,$passphrase) {
$iv = substr(md5("\x1B\x3C\x58".$passphrase, true), 0, 8);
$key = substr(md5("\x2D\xFC\xD8".$passphrase, true) .
md5("\x2D\xFC\xD9".$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;
}
它适用于大多数文件。但是一般来说 SVG 或 XML 文件存在问题。例如,对 SVG 文件的解密会在最后一行给出字符“NUL NUL ...”。如图所示:
【问题讨论】:
-
你用什么来读写文件(以及什么标志)?
-
我很抱歉。我没有给出正确的功能。我必须修改我的问题,但我无法编辑它。
-
加密过滤器在新的 PHP 版本中被弃用:secure.php.net/manual/de/filters.encryption.php
-
来自example 1,
$data = rtrim(stream_get_contents($fp));//trims off null padding -
谢谢@James,这就是问题所在。现在可以了
标签: php xml svg encryption