使用 PHP 过滤器加密 PDF 文件时出现问题


Problems encrypting a PDF file using PHP filters

嗨,我正在尝试编写上传pdf文件的代码,然后将其ftps到NAS框,然后允许用户查看文档 - 即相反,通过FTP从NAS取回文档。 一切都很好。 但是,我现在需要加密 NAs 框上的数据。 我读过过滤器,但我不能让它工作 我唯一见过的东西是文本。 我现在的情况是:

发送代码

 $passphrase = 'My secret';
 /* Turn a human readable passphrase
  * into a reproducable iv/key pair
  */
 $iv = substr(md5('iv'.$passphrase, true), 0, 8);
 $key = substr(md5('pass1'.$passphrase, true) . 
                md5('pass2'.$passphrase, true), 0, 24);
 $opts = array('iv'=>$iv, 'key'=>$key);
 $fp = fopen($file, 'wb');//$file is tne uploaded file
 stream_filter_append($fp, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts);
 fwrite($fp, 'Secret secret secret data');// I know this bit is wrong!!
 fclose($fp);
 if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
       //echo "successfully uploaded $file'n";

所以我用评论改变了它

 $passphrase = 'My secret';
 /* Turn a human readable passphrase
  * into a reproducable iv/key pair
  */
 $iv = substr(md5('iv'.$passphrase, true), 0, 8);
 $key = substr(md5('pass1'.$passphrase, true) . 
                md5('pass2'.$passphrase, true), 0, 24);
 $opts = array('iv'=>$iv, 'key'=>$key);
 $fp = fopen($file, 'wb');
 $fplocal = fopen("templocal.PDF", 'wb');
 stream_filter_append($fplocal, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts);
 fwrite($fplocal, $fp);
 fclose($fplocal);
 fclose($fp);

      // try to upload $file
      if (ftp_put($conn_id, $remote_file, $fplocal,

但它不起作用 - 我做错了什么吗?

$fplocal = fopen("templocal.PDF", 'wb');
stream_filter_append($fplocal, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts);
fwrite($fplocal, file_get_contents($file));
fclose($fplocal);

编辑评论问题

<?php 
// Crypt parameters
    $passphrase = 'thisIsThePassphrase';
    $iv  = substr( 
                md5('iv'.$passphrase, true)
                , 0, 8 
            );
    $key = substr( 
                md5('pad1'.$passphrase, true) . 
                md5('pad2'.$passphrase, true)
               , 0, 24
            );
    $opts = array('iv'=>$iv, 'key'=>$key);
// Input file crypt to outputFile
    $outputFile = fopen("outputFileToWrite.PDF", "wb");
    stream_filter_append(
        $outputFile
        , 'mcrypt.tripledes'
        , STREAM_FILTER_WRITE
        , $opts
    );
    fwrite(
        $outputFile
        , file_get_contents("inputFileToRead.pdf")
    );
    fclose($outputFile);
?>

@mcnd 我找到了包括示例在内的最佳解决方案。 请查看以下链接。 对于所有需要完整概念的 PHP 加密过滤器的人。

加密/解密

谢谢。