locked
how to find the document is password protected without opening it RRS feed

  • Question

  •  

    how to find the file(office documents) is password protected without opening it. Is there any .NET frame work or Win32 API or any scripting language can do this?
    Thursday, September 25, 2008 1:42 PM

Answers

  • Hi Sundara Velu,

     

    http://www.ecma-international.org/publications/standards/Ecma-376.htm

     

    In this link there is a list of standards based on wihch word, excel documents (Office 2007) are composed. There is an SDK (Office Open XML Format SDK v2.0) available for download in Microsoft. Using this, and C# language you can open and manipulate Office 2007 documents.

     

    Using the Part 4 of the standards, search for the ways in which the protection detaills of a word document is stored in the OOXML format. With this information and the SDK you can easily find out whether a doucment is protected or not.

     

    -nk

     

    Thursday, September 25, 2008 8:05 PM
  • May be like this:

     

    using DocumentFormat.OpenXml.Packaging;

    using DocumentFormat.OpenXml.Wordprocessing;

     

    using (WordprocessingDocument wd = WordprocessingDocument.Open(filename, false))

    {

        DocumentProtection dp = wd.MainDocumentPart.DocumentSettingsPart.Settings.GetFirstChild<DocumentProtection>();

        if(dp != null && dp.Enforcement == BooleanValues.One && dp.Edit == DocumentProtectionValues.ReadOnly)

        {

            // Document is password protected for read only.

        }

    }

     

    Friday, September 26, 2008 7:16 AM
  • Hi,

     

    The code I sent should work with Open XML Format SDK 2.0 CTP (sep 2008), with Word 2007 DOCX document.

     

    I have no idea if Open XML Format SDK 2.0 CTP will properly read Word 2003 DOC documents, as they are not Open XML. It may even not work with Word 2003 XML which has some differences with Open XML. But I realy don't know. Sorry.

     

    But you can try it. Hopefully, they may be doing a good job, and convert the older format to the new one... keeping compatibility.

     

    Regards,

     

    Friday, September 26, 2008 12:49 PM

All replies

  • Hi Sundara Velu,

     

    http://www.ecma-international.org/publications/standards/Ecma-376.htm

     

    In this link there is a list of standards based on wihch word, excel documents (Office 2007) are composed. There is an SDK (Office Open XML Format SDK v2.0) available for download in Microsoft. Using this, and C# language you can open and manipulate Office 2007 documents.

     

    Using the Part 4 of the standards, search for the ways in which the protection detaills of a word document is stored in the OOXML format. With this information and the SDK you can easily find out whether a doucment is protected or not.

     

    -nk

     

    Thursday, September 25, 2008 8:05 PM
  • May be like this:

     

    using DocumentFormat.OpenXml.Packaging;

    using DocumentFormat.OpenXml.Wordprocessing;

     

    using (WordprocessingDocument wd = WordprocessingDocument.Open(filename, false))

    {

        DocumentProtection dp = wd.MainDocumentPart.DocumentSettingsPart.Settings.GetFirstChild<DocumentProtection>();

        if(dp != null && dp.Enforcement == BooleanValues.One && dp.Edit == DocumentProtectionValues.ReadOnly)

        {

            // Document is password protected for read only.

        }

    }

     

    Friday, September 26, 2008 7:16 AM
  •  

    hi elfrad,

     

    Thank you very much for your answer. How to find the same for office 2003 documents... will the above code work for office 2003 documents..

    Friday, September 26, 2008 11:57 AM
  • Hi,

     

    The code I sent should work with Open XML Format SDK 2.0 CTP (sep 2008), with Word 2007 DOCX document.

     

    I have no idea if Open XML Format SDK 2.0 CTP will properly read Word 2003 DOC documents, as they are not Open XML. It may even not work with Word 2003 XML which has some differences with Open XML. But I realy don't know. Sorry.

     

    But you can try it. Hopefully, they may be doing a good job, and convert the older format to the new one... keeping compatibility.

     

    Regards,

     

    Friday, September 26, 2008 12:49 PM
  • No luck!

     

    I tryed it, and it didn't work.

    The code throws an "File contains corrupted data." Exception. :-(

     

    Friday, September 26, 2008 12:58 PM
  • is this supprot Word doc 97-2003 also or only 2007 doc file, i am getting error when i am trying to do:

     

    try

    {

     

    string file = "lock|C:\\Print Manager.doc";

     

    string[] args = file.Split('|');

     

    if (args.Length != 2)

    {

     

    Console.WriteLine("Usage: lockdoc lock|unlock filename.docx");

     

    return;

    }

     

    bool isLock = false;

     

    if (args[0].Equals("lock", StringComparison.OrdinalIgnoreCase))

    {

    isLock =

    true;

    }

     

    else if (!args[0].Equals("unlock", StringComparison.OrdinalIgnoreCase))

    {

     

    Console.Error.WriteLine("Wrong action!");

     

    return;

    }

     

    WordprocessingDocument doc = WordprocessingDocument.Open(args[1], true);

    doc.ExtendedFilePropertiesPart.Properties.DocumentSecurity =

     

    new DocumentFormat.OpenXml.ExtendedProperties.DocumentSecurity

    (isLock ?

    "8" : "0");

    doc.ExtendedFilePropertiesPart.Properties.Save();

     

    DocumentProtection dp =

    doc.MainDocumentPart.DocumentSettingsPart

    .Settings.ChildElements.First<

    DocumentProtection>();

     

    if (dp != null)

    {

    dp.Remove();

    }

     

    if (isLock)

    {

    dp =

    new DocumentProtection();

    dp.Edit =

    DocumentProtectionValues.Comments;

    dp.Enforcement = DocumentFormat.OpenXml.Wordprocessing.

    BooleanValues.One;

    dp.Hash =

    "1qYbFPV2bFX/WpB/ii0KfllanDc=";

    dp.Salt =

    "hIqOl99kIr/+N0hyovEq7Q==";

    doc.MainDocumentPart.DocumentSettingsPart.Settings.AppendChild(dp);

    doc.MainDocumentPart.DocumentSettingsPart.Settings.AppendChild(dp);

    }

    doc.MainDocumentPart.DocumentSettingsPart.Settings.Save();

    doc.Close();

    }

     

    catch (Exception ex)

    {

     

    MessageBox.Show(ex.Message);

    }

    Monday, December 28, 2009 5:42 PM
  • The SDK always throw exception for files that protected by password and said it's corrupt document. This is the only clue. But not all corrupt document that can not be opened with the sdk is password protected.
    Wednesday, December 30, 2009 9:55 AM
  • is there any way to access the contents of the XML without using the Open XML sdk ? Assuming I have the correct username and password to access the document ?
    Thursday, February 11, 2010 1:32 AM
  • Well yes, you can use XmlDocument, XPathNavigator, and all the classes used for any XML files.  The Open XML SDK are just OOXML-specific wrappers for those other classes anyway, so you can do anything the SDK does without the SDK, it just means writing more code.
    Thursday, May 20, 2010 2:41 AM