使用PHP筛选器加密PDF文件时出现问题

使用PHP筛选器加密PDF文件时出现问题,php,encryption,filter,ftp,Php,Encryption,Filter,Ftp,您好,我正在尝试编写代码,上传一个pdf文件,然后将其ftps到NAS框中,然后允许用户查看文档-即相反,通过ftp从NAS获取文档。一切都很好。但是,我现在需要加密NAs盒上的数据。我读过一些关于过滤器的文章,但是我不能让它工作,我所看到的唯一的东西就是文本。到目前为止,我的目标是: 发送代码 $passphrase = 'My secret'; /* Turn a human readable passphrase * into a reproducable iv/key pair

您好,我正在尝试编写代码,上传一个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加密过滤器完整概念的人


谢谢。

问题是?我需要它来加密$file而不是创建一个文件并添加秘密…打开输入文件,打开临时输出文件(本地),将过滤器添加到输出文件,将输入文件写入输出文件,关闭两者,上载输出文件。输出文件将是二进制文件,不确定FTP_ASCII是否会产生问题。我更新了代码(如上所述),但它不起作用-我可能在做一些愚蠢的事情:(谢谢。我尝试了,但它不起作用。日志文件说PHP警告:file_get_contents()预计参数1为字符串,谢谢我昨晚在为ftp选择正确的文件后让它工作了。@MCND:我仍然无法以mcrypt格式编写PDF文件。它在fwrite后显示了0kb。你们是如何找到解决方案的?@nikundhimar,完整的示例发布的。嗨@MCND感谢您的快速响应,我已经跟踪了sam但是我的文件在temp文件夹中,所以我使用的文件内容类似于:fwrite($outputFile,file_get_contents(“http://“$\u SERVER['http_HOST']”)。“/nikunj test/”$temp_loc));其中$temp_loc=“temp/”$filename;