0

嗨,我正在尝试编写代码来上传 pdf 文件,然后将其通过 ftp 传输到 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,

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

4

2 回答 2

0
$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);

?>
于 2013-10-23T11:14:38.173 回答
0

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

加密/解密

谢谢你。

于 2015-06-16T10:46:31.947 回答