This example demonstrates how to encrypt and decrypt a document with a password.
File encryption helps to protect our file data. Only someone with the right encryption key (such as a password) can decrypt it.
There are two way to handle file security i.e.
Encryption is used to transform original file content in some unreadable format or unaccessible state using a secret pass code(string)
While decryption is the process to restore encripted file or content into original readable state
We see first is encryption process, in which firstly we see file browser which is the primary file which need to encrypted for security purposes. And Next is password field that is used for securing the file
Further in second part we see decryption process, in which we select a encrypted file and choose a password to decrypt it.
Now, we have versions of this representation in words ,using dropdown you can make a choice within the following
- Word Doc
- Word Docx
- Word ML
- RTF
- MS Text format
- E-book format
- DLS based xml file format
- HTML
After clicking a generate button will launch a file with above choosen version of word , this file will be encrypted or decrypted state.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;
namespace OfficeComponent.Samples
{
class EncryptDecryptExample : WordExampleBase
#if WEB
, IUIExample
#endif
{
#region Inputs
public bool ToEncrypt { get; set; }
public string EncryptFile { get; set; }
public string EncryptPassword { get; set; }
public string DecryptFile { get; set; }
public string DecryptPassword { get; set; }
#endregion
public EncryptDecryptExample()
: base(null,null)
{
}
public EncryptDecryptExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public EncryptDecryptExample(string commonDataPath, string outputDir, string xmlFile)
: base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
#if WEB
ProcessForm();
#endif
WordDocument document;
if (ToEncrypt)
{
if (string.IsNullOrWhiteSpace(EncryptFile))
{
ShowError("Please specify a Word document to encrypt.");
return null;
}
if (string.IsNullOrWhiteSpace(EncryptPassword))
{
ShowError("Please specify encryption password.");
return null;
}
// Create a new instance of the WordDocument class.
document = new WordDocument(EncryptFile);
//Add a new section to the document.
Section section = document.LastSection;
//Adding a new paragraph to the section.
Paragraph paragraph = section.AddParagraph();
//Insert Text into the paragraph
TextRange text = paragraph.AppendText("OfficeComponent Word - This document is password protected.");
text.CharacterFormat.FontSize = 15f;
text.CharacterFormat.FontName = "Tahoma";
// Protect the document
document.Encrypt(EncryptPassword);
}
else
{
if (string.IsNullOrWhiteSpace(DecryptFile))
{
ShowError("Please specify a Word document to decrypt.");
return null;
}
if (string.IsNullOrWhiteSpace(DecryptPassword))
{
ShowError("Please specify decryption password.");
return null;
}
// Create a new instance of the WordDocument class.
document = new WordDocument(DecryptFile, DecryptPassword);
//Add a new section to the document.
Section section = document.LastSection;
//Adding a new paragraph to the section.
Paragraph paragraph = section.AddParagraph();
//Insert Text into the paragraph
TextRange text = paragraph.AppendText("OfficeComponent Word - This document has been decrypted.");
text.CharacterFormat.FontSize = 15f;
text.CharacterFormat.FontName = "Arial";
}
string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
// Save the document.
document.Save(fileName, SaveAsFormat);
// Close the document.
document.Close();
return fileName;
}
public override string ActionTitle
{
get
{
return "Process";
}
}
#if WEB
void ProcessForm()
{
if (ToEncrypt)
EncryptFile = GetPostFile("SourceFile");
else
DecryptFile = GetPostFile("SourceFile");
}
#endif
}
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Word
Namespace OfficeComponent.Samples
#If WEB Then
Friend Class EncryptDecryptExample
Inherits WordExampleBase
Implements IUIExample
#Else
Friend Class EncryptDecryptExample
Inherits WordExampleBase
#End If
#Region "Inputs"
Private privateToEncrypt As Boolean
Public Property ToEncrypt() As Boolean
Get
Return privateToEncrypt
End Get
Set(ByVal value As Boolean)
privateToEncrypt = value
End Set
End Property
Private privateEncryptFile As String
Public Property EncryptFile() As String
Get
Return privateEncryptFile
End Get
Set(ByVal value As String)
privateEncryptFile = value
End Set
End Property
Private privateEncryptPassword As String
Public Property EncryptPassword() As String
Get
Return privateEncryptPassword
End Get
Set(ByVal value As String)
privateEncryptPassword = value
End Set
End Property
Private privateDecryptFile As String
Public Property DecryptFile() As String
Get
Return privateDecryptFile
End Get
Set(ByVal value As String)
privateDecryptFile = value
End Set
End Property
Private privateDecryptPassword As String
Public Property DecryptPassword() As String
Get
Return privateDecryptPassword
End Get
Set(ByVal value As String)
privateDecryptPassword = value
End Set
End Property
#End Region
Public Sub New()
MyBase.New(Nothing,Nothing)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
#If WEB Then
ProcessForm()
#End If
Dim document As WordDocument
If ToEncrypt Then
If String.IsNullOrWhiteSpace(EncryptFile) Then
ShowError("Please specify a Word document to encrypt.")
Return Nothing
End If
If String.IsNullOrWhiteSpace(EncryptPassword) Then
ShowError("Please specify encryption password.")
Return Nothing
End If
' Create a new instance of the WordDocument class.
document = New WordDocument(EncryptFile)
'Add a new section to the document.
Dim section As Section = document.LastSection
'Adding a new paragraph to the section.
Dim paragraph As Paragraph = section.AddParagraph()
'Insert Text into the paragraph
Dim text As TextRange = paragraph.AppendText("OfficeComponent Word - This document is password protected.")
text.CharacterFormat.FontSize = 15F
text.CharacterFormat.FontName = "Tahoma"
' Protect the document
document.Encrypt(EncryptPassword)
Else
If String.IsNullOrWhiteSpace(DecryptFile) Then
ShowError("Please specify a Word document to decrypt.")
Return Nothing
End If
If String.IsNullOrWhiteSpace(DecryptPassword) Then
ShowError("Please specify decryption password.")
Return Nothing
End If
' Create a new instance of the WordDocument class.
document = New WordDocument(DecryptFile, DecryptPassword)
'Add a new section to the document.
Dim section As Section = document.LastSection
'Adding a new paragraph to the section.
Dim paragraph As Paragraph = section.AddParagraph()
'Insert Text into the paragraph
Dim text As TextRange = paragraph.AppendText("OfficeComponent Word - This document has been decrypted.")
text.CharacterFormat.FontSize = 15F
text.CharacterFormat.FontName = "Arial"
End If
Dim fileName As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
' Save the document.
document.Save(fileName, SaveAsFormat)
' Close the document.
document.Close()
Return fileName
End Function
Public Overrides ReadOnly Property ActionTitle() As String
Get
Return "Process"
End Get
End Property
#If WEB Then
Private Sub ProcessForm()
If ToEncrypt Then
EncryptFile = GetPostFile("SourceFile")
Else
DecryptFile = GetPostFile("SourceFile")
End If
End Sub
#End If
End Class
End Namespace