In this
example we explain that how to encrypt and Decrypt XML file in asp.net with
C#.in previous we already explain that how to Encrypt and Decrypt Password when
user is register to website then it's password are stored in SqlServer database
in Encrypt format and when user is login to website then it will fetch the
Encrypt password from the database and Decrypt it and match with user enter
password.
So this is simple thing but now in this example we will explain how
to encrypt and Decrypt whole XML file in asp.net. The main advantages to
Encrypt and Decrypt XML file is that even user cannotsee data in XML file
because data are stored in Encrypt format so it cannot be read by any user.
When any operation is performed in XML file then it will first Decrypt XML file
and then perform operation. So this is the best facility for any application
for better security.
protected void Page_Load(object sender, EventArgs e) { }
protected void EncryptFileButton_Click(object sender, EventArgs e)
{
//Get the Input File Name and Extension.
string fileName = Path.GetFileNameWithoutExtension(flupld_xmlfile.PostedFile.FileName);
string fileExtension = Path.GetExtension(flupld_xmlfile.PostedFile.FileName);
//Build the File Path for the original (input) and the encrypted (output) file.
string input = Server.MapPath("~/Files/") + fileName + fileExtension;
string output = Server.MapPath("~/Files/") + fileName + "_enc" + fileExtension;
//Save the Input File, Encrypt it and save the encrypted file in output path.
flupld_xmlfile.SaveAs(input);
this.Encrypt(input, output);
//Download the Encrypted File.
Response.ContentType = flupld_xmlfile.PostedFile.ContentType;
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(output));
Response.WriteFile(output);
Response.Flush();
//Delete the original (input) and the encrypted (output) file.
File.Delete(input);
File.Delete(output);
Response.End();
}
private void Encrypt(stringinputFilePath, stringoutputfilePath)
{
stringEncryptionKey = "MAKV2SPBNI99212";
using (Aesencryptor = Aes.Create())
{
Rfc2898DeriveBytespdb = newRfc2898DeriveBytes(EncryptionKey, newbyte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16);
using (FileStreamfsOutput = newFileStream(outputfilePath, FileMode.Create))
{
using (CryptoStreamcs = newCryptoStream(fsOutput, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
using (FileStreamfsInput = newFileStream(inputFilePath, FileMode.Open))
{
int data; while ((data = fsInput.ReadByte()) != -1)
{
cs.WriteByte((byte)data);
}
}
}
}
}
}
protected void DecryptFileButton_Click(object sender, EventArgs e)
{
//Get the Input File Name and Extension
string fileName = Path.GetFileNameWithoutExtension(flupld_xmlfile.PostedFile.FileName);
string fileExtension = Path.GetExtension(flupld_xmlfile.PostedFile.FileName);
//Build the File Path for the original (input) and the decrypted (output) file
string input = Server.MapPath("~/Files/") + fileName + fileExtension;
string output = Server.MapPath("~/Files/") + fileName + "_dec" + fileExtension;
//Save the Input File, Decrypt it and save the decrypted file in output path.
flupld_xmlfile.SaveAs(input);
this.Decrypt(input, output);
//Download the Decrypted File.
Response.Clear();
Response.ContentType = flupld_xmlfile.PostedFile.ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(output));
Response.WriteFile(output);
Response.Flush();
//Delete the original (input) and the decrypted (output) file.
File.Delete(input);
File.Delete(output);
Response.End();
}
private void Decrypt(stringinputFilePath, stringoutputfilePath)
{
string EncryptionKey = "MAKV2SPBNI99212";
using (Aesencryptor = Aes.Create())
{
Rfc2898DeriveBytespdb = newRfc2898DeriveBytes(EncryptionKey, newbyte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16);
using (FileStreamfsInput = newFileStream(inputFilePath, FileMode.Open))
{
using (CryptoStreamcs = newCryptoStream(fsInput, encryptor.CreateDecryptor(), CryptoStreamMode.Read))
{
using (FileStreamfsOutput = newFileStream(outputfilePath, FileMode.Create))
{
int data;
while ((data = cs.ReadByte()) != -1)
{
fsOutput.WriteByte((byte)data);
}
}
}
}
}
}