Encrypt Word with Custom Password in C#, VB.NET

Word encryption, one method to protect Word document, requires users to give the document a password. Without the password, encrypted document cannot be opened. Solution in this guide demonstrates how to encrypt Word document with custom password in C# and VB.NET via Spire.Doc for .NET.

Spire.Doc for .NET, specializing in performing Word processing tasks for .NET, provides a Document.Encrypt method which enables users to encrypt Word. The overload passed to this method is string password. Firstly, load the Word document which is needed to protect. Secondly, invoke Document.Encrypt method to encrypt with password. Thirdly, save the encrypt document and launch for viewing. After debugging, a dialog box pops up and requires the password. Enter the password to open the document and the document information will be shown as following to tell users that it is encrypted.

Encrypt Word Document

Download and install Spire.Doc for .NET and use the following code to encrypt Word.

[C#]
using Spire.Doc;

namespace Encryption
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx");

            //Encrypt
            document.Encrypt("eiceblue");

            //Save and Launch
            document.SaveToFile("Encryption.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Encryption.docx");
        }
    }
}
[VB.NET]
Imports Spire.Doc

Namespace Encryption
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx")

            'Encrypt
            document.Encrypt("eiceblue")

            'Save and Launch
            document.SaveToFile("Encryption.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Encryption.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, an easy-to-use component to operate Word document, allows developers to fast generate, write, edit and save Word (Word 97-2003, Word 2007, Word 2010) in C# and VB.NET for .NET, Silverlight and WPF.