none
Set Microsoft Word Document password encryption algorithm using Word Interop SetPasswordEncryptionOptions method RRS feed

  • Question

  • Hi Team,

    I am using the Word Interop dll with C# to set password encryption algorithm  for Word document.

    I am able to set password encryption algorithm using Microsoft.Office.Interop.Word.Document object SetPasswordEncryptionOptions  method with arguments as "Microsoft Enhanced Cryptographic Provider v1.0", "RC4", 128.  But when we change the arguments like "Office","OfficeStandard", 0, it gives error of "Insufficient memory". The issue occurs on Microsoft Office 2013 machine. It works fine on Microsoft Office 2003. 

    object missing = System.Reflection.Missing.Value;
                object readOnly = true;
                object visible = false;
                object _Password = "abc";
                object enforceStyleLock = false;
                object PasswordEncryptionFileProperties = false;
                
                Application application = new Application();
                Microsoft.Office.Interop.Word.Document document = null;
    
                try
                {
    
                    document = application.Documents.Open("C:\\Documents\\sample.doc", missing, readOnly, missing, _Password,missing, missing, missing, missing, missing, missing, visible, visible, missing, missing, missing);
    
                   document.SetPasswordEncryptionOptions("Office","OfficeStandard", 0, null, ref PasswordEncryptionFileProperties);
    
                    document.SaveAs2("C:\\Documents\\Interopoutput.doc");
                    
    				  if (document != null)
                        document.Close();
                    application.Quit();
    				
                    Console.WriteLine("Completed");
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    string message = ex.Message + System.Environment.NewLine + ex.InnerException + ex.StackTrace;
                    Console.WriteLine(message);
                }

    How to set password encryption algorithm using "Office","OfficeStandard", 0 for Office 2013 docs. 

    Regards,

    Shrikant

    Tuesday, January 19, 2016 10:35 AM

All replies

  • One of the office forums could be of a better assistance on this, like Word for Developers forum.

    Also check this note from msdn:

    "For enhanced security, do not use Weak Encryption (XOR) (also called "OfficeXor") or "Office97/2000 Compatible" (also called "OfficeStandard") algorithms."


    Fouad Roumieh


    Tuesday, January 19, 2016 10:25 AM
  • Thanks for the reply and information. I will post this on 'Word for developers forum' also to get the help on this issue.

    Meanwhile, I have also tried to set AES algortihm but still it shows the same error. 

    Arguments ::  "Microsoft Enhanced Cryptographic Provider v1.0", "AES", 128

    Regards,

    Shrikant


    Tuesday, January 19, 2016 10:43 AM
  • Hi ShrikantCC,

    First, there are only 4 parameters for Document.SetPasswordEncryptionOptions and the PasswordEncryptionKeyLength parameter must be a multiple of 8, starting at 40.

    And for the "Microsoft Enhanced Cryptographic Provider v1.0" provider, there is no AES algorithm in this provider. We can use Microsoft Enhanced RSA and AES Cryptographic Provider for the AES algorithm. And here is an code sample using VBA changing the encrypt algorithm to "AES 128" for your reference:

    ActiveDocument.SetPasswordEncryptionOptions "Microsoft Enhanced RSA and AES Cryptographic Provider", "AES 128", 128

    Note: For enhanced security, do not use Weak Encryption (XOR) (also called "OfficeXor") or "Office97/2000 Compatible" (also called "OfficeStandard") algorithms.

    In addition, here are some helpful articles about cryptography and encryption for your reference:
    Plan cryptography and encryption settings for Office 2013

    Microsoft Cryptographic Service Providers

    Document.SetPasswordEncryptionOptions Method

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 20, 2016 5:33 AM
    Moderator
  • Thanks for the reply. I tried to set algorithm using =>

    ActiveDocument.SetPasswordEncryptionOptions "Microsoft Enhanced RSA and AES Cryptographic Provider", "AES 128", 128

    But still it shows error of 'Insufficient memory' to me. Document for which i'm trying to change encryption algorithm is password protected with RC4 encryption.

    Is there any way to attach test word document to post related to this issue for your reference?



    • Edited by ShrikantCC Thursday, January 21, 2016 11:10 AM
    Thursday, January 21, 2016 9:59 AM
  • Hi,

    This is depends on whether the computer have the Microsoft Enhanced RSA and AES Cryptographic Provider. You can check the exits provider via the registry key like path below:

    HKEY_LOCAL_MACHINE\Comm\Security\Crypto\Defaults\Provider\

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 22, 2016 10:03 AM
    Moderator
  • Thanks for the reply.

    Following path is not available on machine.

    HKEY_LOCAL_MACHINE\Comm\Security\Crypto\Defaults\Provider\

    But 

    HKEY_LOCAL_MACHINE\Microsoft\Cryptography\Defaults\Provider\

    this path is available with providers. 

    Regards,

    Shrikant


    Friday, January 22, 2016 11:18 AM
  • Hi Shrikant,

    Thanks for the figure.

    To narrow down this issue, I suggest that you to verify whether the specific algorithm was able to use the other tools instead of Office application.

    If it is ok, you can try to repair Office application to see whether this issue was fixed.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Saturday, January 30, 2016 8:02 AM
    Moderator