How to encrypt word document?

Hi team,

I found that we can use documentprotection node to be added to settings through openxml to encrypt document. Is there any way to achieve this using aspose.words?

Thanks in advance

Hi Sreedivya,

Thanks for your inquiry. Please refer to the following article:
https://docs.aspose.com/words/java/protect-or-encrypt-a-document/

You can also use WriteProtection.SetPassword method to set the write protection password for the document. If a password is set, Microsoft Word will require the user to enter it or open the document as read-only.

Best regards,

Thanks Awais for your quick reply.
Currently we are protecting document using following code in openxml

theDoc.MainDocumentPart.DocumentSettingsPart.Settings.AppendChild(
new DocumentProtection()
{
    Edit = DocumentProtectionValues.ReadOnly,
    Enforcement = OnOffValue.FromBoolean(true),
    CryptographicProviderType = CryptProviderValues.xxx,
    CryptographicAlgorithmClass = CryptAlgorithmClassValues.xxx,
    CryptographicAlgorithmType = CryptAlgorithmValues.xxx,
    CryptographicAlgorithmSid = x,
    CryptographicSpinCount = xxxx,
    Hash = new Base64BinaryValue() { Value = xxx },
    Salt = new Base64BinaryValue() { Value = xxx }
});

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

Can we achieve this using aspose in one or other way?

Thanks

Hi Sreedivya,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Your expected document.
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your expected document on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Hi,

I have added restrict editing access for the document doc.docx I have added below with password.

When I renamed docx to zip file, I have generated doc-copy.zip . In that zip folder, you can see settings.xml file in doc - Copy.zip\word . I can see following node in settings.xml file

<w:documentProtection w:salt="5F/p8fr8AV8aqrvLVTpRWA==" w:hash="vaoDnGyMFnafN2yAo/EHYCd4rHQ=" w:cryptSpinCount="100000" w:cryptAlgorithmSid="4" w:cryptAlgorithmType="typeAny" w:cryptAlgorithmClass="hash" w:cryptProviderType="rsaFull" w:enforcement="1" w:edit="readOnly"/>

Is there a way to set this salt,hash,algorithm value using Aspose?

Thanks

Hi Sreedivya,

Please tell if the following code is acceptable for your requirements?

Document doc = new Document(); 
doc.Protect(ProtectionType.ReadOnly, "pwd");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Test123");
builder.MoveToDocumentStart();
EditableRangeStart start = builder.StartEditableRange();
EditableRangeEnd end = builder.EndEditableRange();
doc.Save(MyDir + @"16.7.0.docx");

Best regards,

I tried this and it worked well. Is there any way to change algorithm attribute to some custom value?

Hi,

I am in coordination with product team to get answer pertaining to your queries. Soon you will be updated with the required information.

Best regards,

Thanks, Looking forward to receive reply soon.

Hi Sreedivya,

It is to update you that all Aspose.Words can do with protecting is described here: https://docs.aspose.com/words/net/protect-or-encrypt-a-document/

Or if you just want to set password, you can use “OoxmlSaveOptions.Password Property” https://reference.aspose.com/words/net/aspose.words.saving/ooxmlsaveoptions/password/

This property uses ECMA376 Standard encryption algorithm.

If you need to make sure the document is really protected from changes, consider digitally signing the document. Aspose.Words supports detecting digital signatures for DOC, OOXML and ODT documents. Aspose.Words also preserves a digital signature applied to the VBA project (macros) contained in a document. For further details see the Working with Digital Signatures article.

Best regards,