Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a Word Document is password protected without opening it

I'm using the Microsoft Office Interop namespace in order to password protect Word documents. I first create an instance of Word as a background process and gather the list of Word documents I want to protect. The issue I have is that when the application gets to an already secure document it opens with a prompt for the set password. After dismissing this it runs through the rest of the documents with the instance of Word now in the foreground.

I would like to skip past the previously protected documents. My code is currently as follows:

    private void Protect_Word(FileInfo file)
    {
            Word.Document doc = wrd.Documents.Open(file.FullName);
            if (!doc.HasPassword)
            { 
                doc.Password = this.passwordBox.Text;
                doc.Save();

            }
            ((Microsoft.Office.Interop.Word._Document)doc).Close(0);
    }

The problem with this setup is that the password for a document is being asked for before I can call HasPassword. Is there a way I can programatically check the document for a password prior to actually opening it?

like image 334
Chrayfish Avatar asked Oct 18 '22 09:10

Chrayfish


2 Answers

Also experiencing this problem ... I came up with the "dirty solution" - pass invalid password for "Word" (or Excel) and if the document with a password, WINWORD.EXE will process this option will try to apply to the document, the password does not match, will generate a COM exception, we catch the exception and understand that document WITH A PASSWORD! Otherwise, if the document does not have a password, the Word processor itself will ignore the transmitted password (I understood this after checking it experimentally ... )

        Word.Application app = null;
        Word.Documents docs = null;
        try
        {
            app = new Word.Application();
            app.Visible = false;
            docs = app.Documents;
            // перебираем список файлов
            for (int i = 0; i < listFilesWord.Count; i++)
            {
                Word.Document doc = null;
                try
                {
                    doc = docs.Open(
                        listFilesWord[i].FullName,
                        Word.WdSaveFormat.wdFormatDocument,
                        (object)false,
                        Type.Missing,
                        " ",
                        Type.Missing,
                        Type.Missing,
                        Type.Missing,
                        Type.Missing,
                        Type.Missing,
                        Type.Missing,
                        (object)false,
                        Type.Missing,
                        Type.Missing,
                        (object)true,
                        Type.Missing);
                    doc.Save();
                    doc.Close();
                }
                catch (COMException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (doc != null) Marshal.ReleaseComObject(doc);
                }
            }
            app.Quit();
        }
        finally
        {
            if (docs != null) Marshal.ReleaseComObject(docs);
            if (app != null) Marshal.ReleaseComObject(app);
        }
like image 119
KUL Avatar answered Oct 26 '22 23:10

KUL


If you say "without opening it" you must mean "without opening it in Word" (because you must inspect the file contents which requires opening it on the file level.)

It looks as if the docx file format, like others, has an identifying signature at the beginning, namely the zip file signature 50 4b 03 04. (Are there endianness issues? I'm on little-endian Intel.) This probably reflects the choice made by MS to store office documents as an XML file collection in a standard zip file.

When the file is encrypted though the first bytes change to something else (in my case to d0 cf 11 e0 which the File Signature Database identifies as an Access project file for some reason).

This just may be sufficient for telling "normal" from encrypted Word documents, but you may want to do more research first, both by parsing the relevant documentation and by cross checking a typical set of documents from the source which you want to work with.

like image 32
Peter - Reinstate Monica Avatar answered Oct 26 '22 22:10

Peter - Reinstate Monica