检测受密码保护的PPT和XLS文档

我找到了这个答案https://stackoverflow.com/a/14336292/1537195 ,这为检测DOC和XLS文件的密码保护提供了一个很好的方法。

//Flagged with password if (bytes.Skip(0x20c).Take(1).ToArray()[0] == 0x2f) return true; //XLS 2003 if (bytes.Skip(0x214).Take(1).ToArray()[0] == 0x2f) return true; //XLS 2005 if (bytes.Skip(0x20B).Take(1).ToArray()[0] == 0x13) return true; //DOC 2005 

但它似乎并没有覆盖所有XLS文件,我也在寻找一种以相同方式检测PPT文件的方法。 无论如何知道这些文件类型要查看哪些字节?

我保存了一个PowerPoint演示文稿,如.ppt和.pptx,无论是否需要密码打开它们,用7-Zip打开它们,并得出初步的结论:

  • 没有密码的.pptx文件始终使用标准的.zip文件格式
  • .ppt文件是CompoundDocuments
  • 带密码的.pptx文件也是CompoundDocuments
  • 所有密码版的CompoundDocuments都包含一个名为* Encrypt *的条目

要使此代码运行,您需要安装NuGet包OpenMcdf。 这是我能找到的第一个读取CompoundDocuments的C#库。

 using OpenMcdf; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace _22916194 { //http://stackoverflow.com/questions/22916194/detecing-password-protected-ppt-and-xls-documents class Program { static void Main(string[] args) { foreach (var file in args.Where(File.Exists)) { switch (Path.GetExtension(file)) { case ".ppt": case ".pptx": Console.WriteLine($"* {file} " + (HasPassword(file) ? "is " : "isn't ") + "passworded"); Console.WriteLine(); break; default: Console.WriteLine($" * Unknown file type: {file}"); break; } } Console.ReadLine(); } private static bool HasPassword(string file) { try { using (var compoundFile = new CompoundFile(file)) { var entryNames = new List(); compoundFile.RootStorage.VisitEntries(e => entryNames.Add(e.Name), false); //As far as I can see, only passworded files contain an entry with a name containing Encrypt foreach (var entryName in entryNames) { if (entryName.Contains("Encrypt")) return true; } compoundFile.Close(); } } catch (CFFileFormatException) { //This is probably a .zip file (=unprotected .pptx) return false; } return false; } } } 

您应该能够扩展此代码以处理其他Office格式。 顶部的结论应该是正确的,除了你需要在CompoundDocument中查找一些其他数据而不是包含* Encrypt *的文件名(我快速浏览了.doc文件,它看起来似乎没有完全相同)。