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

时间:2014-04-07 15:04:19

标签: c# office-interop

我找到了这个答案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文件的方法。无论如何都知道要查看这些文件类型的哪些字节?

1 个答案:

答案 0 :(得分:0)

我将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<string>();
                    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文件,它似乎没有完全相同的工作)。

相关问题