1

问题 :

我需要对服务器上的文件进行加密,它适用于 .txt、.doc、.xls、.ppt,但不适用于 .docx、.xlsx 和 .pptx。

当我尝试编辑 docx(或 xlsx、pptx)时的问题是文件被我加密/解密的方式损坏,因为它不是编辑 docx 的正确方法。因此,当 Microsoft Word 尝试打开它时,它说它已损坏,它以“Document1.docx”而不是“MyFileName.docx”的形式打开它,保存时我必须再次提供名称,而使用 pptx 我什至必须提供文档所在的 webdav 文件夹的路径。

问题 :

有没有办法让它保存在正确的位置而无需输入路径?

代码 :

这是我用来加密文件的代码:

$ext = explode( '.', basename($path));
if (in_array("doc", $ext) || in_array("docx", $ext)) {
    $handle = fopen("$davPath/$path", "rb");
    $data_file = fread($handle, filesize("$davPath/$path"));
    fclose($handle);
} else {            
    $data_file = file_get_contents("$davPath/$path");
}

$encrypt_data_file = $encryption->encrypt($data_file);

if (file_put_contents("$davPath/encrypt_" . basename($path),$encrypt_data_file)) {
    unlink("$davPath/" . basename($path));
    rename("$davPath/encrypt_" . basename($path),"$davPath/" . basename($path));
    return true;
} else {
    return false;
}

这是我用来解密它们的代码:

$ext = explode( '.', basename($uri));
if(is_file($davPath."/".$uri)) {
    if (in_array("doc", $ext) || in_array("docx", $ext)) {
        $handle = fopen("$davPath/$uri", "rb");
        $data_file = fread($handle, filesize("$davPath/$uri"));
        fclose($handle);
    } else {
        $data_file = file_get_contents("$davPath/$uri");
    }   
}
if ($data_file != false) {
    $decrypt_data_file = $encryption->decrypt($data_file);

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($uri));
    header('Content-Location: '.$_SERVER['SCRIPT_URI']);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    ob_clean();
    flush();
    echo $decrypt_data_file;
    return false;
}

PS:我确实找到了一种解决方法,其中包括在修改期间在服务器上解密文件,但我真的不想这样做。

4

2 回答 2

1

感谢 edi9999 的建议,我使用十六进制编辑器查看未加密/解密的 docx 和加密/解密的 docx 之间的差异。

唯一的区别是在第一个(未损坏)的末尾有 3 次 '00' 不在损坏的那个中。

没有损坏的 docx 的解决方案是在我的解密数据的末尾添加 3 倍“\0”。现在它工作得很好!

对于 docx 和 pptx,它是“\0”的 3 倍,对于 xlsx,它是 4 倍。

于 2014-07-04T13:14:15.953 回答
0

您的问题已解决,但我想添加一个答案。

当您的 docx 损坏时,可以通过以下步骤找出问题所在:

首先,尝试解压缩 zip。如果它确实有效,那么您的问题出在 docx 的内容上。如果解压缩不起作用,则您的 zip 似乎已损坏

docx内容的问题

当您打开 docx 时,如果 zip 没有损坏,word 可能会告诉您问题出在哪里。

例如,它会告诉你:Parse error on line 213 of document.xml

这是解压缩后 docx 的“正常”结构。

+--docProps
|  +  app.xml
|  \  core.xml
+  res.log
+--word //this folder contains most of the files that control the content of the document
|  +  document.xml //Is the actual content of the document
|  +  endnotes.xml
|  +  fontTable.xml
|  +  footer1.xml //Containst the elements in the footer of the document
|  +  footnotes.xml
|  +--media //This folder contains all images embedded in the word
|  |  \  image1.jpeg
|  +  settings.xml
|  +  styles.xml
|  +  stylesWithEffects.xml
|  +--theme
|  |  \  theme1.xml
|  +  webSettings.xml
|  \--_rels
|     \  document.xml.rels //this document tells word where the images are situated
+  [Content_Types].xml
\--_rels
   \  .rels

docx 标签 wiki所示。

损坏的拉链

如果 zip 已损坏,在大多数情况下,它们是文件开头或结尾的一些不应该存在的字符(或者应该和不存在)。

最好是拥有同一文档的有效 docx,并使用两个文档的十六进制表示来查看有什么区别。

我通常为此使用该hexdiff工具(apt-get install hexdiff)。

这通常会显示额外字符的位置。

很多时候,问题是你有错误的标题。

于 2014-07-07T12:56:28.307 回答