Word Encryption for DOCX in C# and VB.NET

DOCX encryption enables you to securely protect the content of your Word document from unwanted viewers by encrypting it with a password. The passwords are case-sensitive and they can't be empty.

You can use the DocxLoadOptions.IsEncrypted static methods to check if a Word document is encrypted.

Location of encryption option in Microsoft Word application

To read an encrypted Word document, you'll need to provide a valid password by using the DocxLoadOptions.Password property. In case of an invalid password, GemBox.Document won't be able to decrypt the file and thus a SecurityException will occur when trying to load it.

To write an encrypted Word document, you'll need to provide a password by using the DocxSaveOptions.Password property.

The following example shows how you can load an encrypted DOCX file and save it as an encrypted DOCX file with a different password in C# and VB.NET.

Encrypting and decrypting Word file of DOCX format in C# and VB.NET
Screenshot of Word document's encryption
Upload your file (Drag file here)
using GemBox.Document;

class Program
    static void Main()
        // If using Professional version, put your serial key below.

        string inputPassword = "%InputPassword%";
        string outputPassword = "%OutputPassword%";

        var document = DocumentModel.Load("%InputFileName%",
            new DocxLoadOptions() { Password = inputPassword });

        document.Save("DOCX Encryption Output.docx",
            new DocxSaveOptions() { Password = outputPassword });
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.

        Dim inputPassword As String = "%InputPassword%"
        Dim outputPassword As String = "%OutputPassword%"

        Dim document = DocumentModel.Load("%InputFileName%",
            New DocxLoadOptions With {.Password = inputPassword})

        document.Save("DOCX Encryption Output.docx",
            New DocxSaveOptions With {.Password = outputPassword})

    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.

Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.