如何检测.doc密码保护


How to detect .doc password protection

以下答案允许在将.docx文件移植到PHP后检测受密码保护的文件:https://stackoverflow.com/a/14347730/1794894

$content = utf8_encode(file_get_contents($absolutePath));
if (mb_substr($content, 0, 2) == "ÐÏ") {
    # DOC/XLS 2007+
    $start = str_replace("'x00", " ", mb_substr($content, 0, 2000));
    if (mb_strstr($start, 'E n c r y p t e d P a c k a g e') !== false) {
        return true;
    }
    if ($extension == 'doc') {
        return true;
    }
}

如何进行.doc特定检查?.doc文件是否也有特定的字节序列?还是只依赖文件前两个字符的ÐÏ检查就足够了?

或者,在密码保护的.doc的情况下,位置0x20B处的字符是否始终等于0x13?

根据问题中帖子中的C#示例解决了问题。

我们不能使用COM对象,大多数服务器都不运行MS Word。

请参阅Gist片段:https://gist.github.com/rvanlaak/06ca1b65658a91240362

应该是这样的。。。

<?php
  $word=new COM("word.application") or die("Cannot create Word object");
  $word->Visible=false;
  $word->WindowState=2;
  $word->DisplayAlerts=false;
  $doc = $word->Document->Open("/yourFile.doc");
  $passwordProtect = $doc->Document->HasPassword;//true or false
  $word->ActiveDocument->Close(false);
  $word->Quit();
  $word->Release();
  $word=null;
?>

我无法测试此代码,希望它能有所帮助。。。