Encript the MS word document in c#

Posted by Bageshkumarbagi under ASP.NET category on | Points: 40 | Views : 853
Design Page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<form id="form1" runat="server">
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />

<hr />
<asp:Button ID="btnEncrypt" Text="Encrypt File" runat="server" OnClick="EncryptFile" />


using System.Security.Cryptography;

protected void EncryptFile(object sender, EventArgs e)
//Get the Input File Name and Extension.
string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);

//Build the File Path for the original (input) and the encrypted (output) file.
string input = @"D:\" + fileName + fileExtension;
string output = @"D:\" + fileName + "_enc" + fileExtension;

//Save the Input File, Encrypt it and save the encrypted file in output path.
this.Encrypt(input, output);

//Download the Encrypted File.
Response.ContentType = FileUpload1.PostedFile.ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(output));

//Delete the original (input) and the encrypted (output) file.


private void Encrypt(string inputFilePath, string outputfilePath)
string EncryptionKey = "MAKV2SPBNI99212";
using (Aes encryptor = Aes.Create())
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create))
using (CryptoStream cs = new CryptoStream(fsOutput, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
int data;
while ((data = fsInput.ReadByte()) != -1)

Comments or Responses

Login to post response