Subhash Sharma

Subhash Sharma
Subhash Sharma

This is Subhash Sharma(Software Engineer) Blog

Welcome to this blog and find every solution.............

Search This Blog

Software Engineer(Subhash Sharma)

Software Engineer(Subhash Sharma)
Software Engineer

Monday, December 27, 2010

Encrypt and Decrypt Xml and Text File

using System.Security.Cryptography;

/* System.Security.Cryptography namespace
Microsoft provides cryptographic services, including secure encoding and decoding of data.
*/
//hash algorithm are applied to encrpt the string and store in un-readable format..

public XmlDocument OpenXmlDocument(String filePath)//Decrypt Xml File
{
bool useHashing = true;
string Key = "Subhash";
byte[] keyArray;
StreamReader sr = new StreamReader(filePath);//here we read the file of given Path
String str = sr.ReadToEnd();
sr.Close();
byte[] toEncryptArray = Convert.FromBase64String(str);//convert string into ByteArray

if (useHashing){

//All Hash function take input of type Byte[]
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();

//The ComputeHash method accepts only an array of bytes or a stream..

keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();//Data Encryption/Decpription Standard
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();//for Decrypting Data
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
string xmlData = UTF8Encoding.UTF8.GetString(resultArray);
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmlData);

return xDoc;

}
public void SaveXmlDocument(XmlDocument xmldocument, string FilePath, bool EncryptXmlFile)//EncryptXmlFile will be true
{
// bool useHashing = true;
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(xmldocument.OuterXml);//convert Xml file into ByteArray
string Key = "Subhash"; //set the key here for encrypting
if (EncryptXmlFile)
{
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();

//The ComputeHash method accepts only an array of bytes or a stream..

keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);//convert into ByteArray

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;//Gets or sets the mode for operation of the symmetric algorithm
tdes.Padding = PaddingMode.PKCS7;//Gets or sets the padding mode used in the symmetric algorithm.

ICryptoTransform cTransform = tdes.CreateEncryptor();//for Encrypting Data.
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
StreamWriter _sw = new StreamWriter(FilePath);//For write Encrypted file into given Path
_sw.WriteLine(Convert.ToBase64String(resultArray, 0, resultArray.Length));
_sw.Flush();
_sw.Close();
_sw.Dispose();
}
public void SaveTextDocument(string Textdocument, string FilePath, bool EncryptTextFile)//EncryptTextFile will be true
{
// bool useHashing = true;
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(Textdocument);//convert Text file into ByteArray
string Key = "Subhash"; //set the key here for encrypting
if (EncryptTextFile)
{
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();

//The ComputeHash method accepts only an array of bytes or a stream..

keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);//convert into ByteArray

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;//Gets or sets the mode for operation of the symmetric algorithm
tdes.Padding = PaddingMode.PKCS7;//Gets or sets the padding mode used in the symmetric algorithm.

ICryptoTransform cTransform = tdes.CreateEncryptor();//for Encrypting Data.
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
StreamWriter _sw = new StreamWriter(FilePath);//For write Encrypted file into given Path
_sw.WriteLine(Convert.ToBase64String(resultArray, 0, resultArray.Length));
_sw.Flush();
_sw.Close();
_sw.Dispose();
}


protected void btnGenerateXml_Click(object sender, EventArgs e)
{
string FilePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "QueryMaster.xml");
//........for check if xml file before Encryption
StreamReader sr = new StreamReader(FilePath);
String str = sr.ReadToEnd();
sr.Close();
string[] k = str.Split(' ');
string s = k[0];
if (str.Contains("<"))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(FilePath);
SaveXmlDocument(xmlDoc, FilePath, true);
}

//...........................
//if (s == " // {

// }

}


protected void Read_Click(object sender, EventArgs e)
{
string FilePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "QueryMaster.xml");
XmlDocument xResultDoc = OpenXmlDocument(FilePath);
}
protected void btnGenerateText_Click(object sender, EventArgs e)
{
string FilePath1 = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "a.txt");
StreamReader streamReader = new StreamReader(FilePath1);
string text = streamReader.ReadToEnd();
streamReader.Close();
SaveTextDocument(text, FilePath1, true);
}
protected void btnReadText_Click(object sender, EventArgs e)
{
string FilePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "a.txt");
string xResultDoc = OpenTextDocument(FilePath);
}
public string OpenTextDocument(String filePath) //Decrypt text File
{
bool useHashing = true;
string Key = "Subhash";
byte[] keyArray;
StreamReader sr = new StreamReader(filePath);//here we read the file of given Path
String str = sr.ReadToEnd();
sr.Close();
byte[] toEncryptArray = Convert.FromBase64String(str);//convert string into ByteArray

if (useHashing)
{

//All Hash function take input of type Byte[]
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();

//The ComputeHash method accepts only an array of bytes or a stream..

keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();//Data Encryption/Decpription Standard
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();//for Decrypting Data
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();

string s= UTF8Encoding.UTF8.GetString(resultArray);
return s;


//string xmlData = UTF8Encoding.UTF8.GetString(resultArray);
//XmlDocument xDoc = new XmlDocument();
//xDoc.LoadXml(xmlData);
//return xDoc;

}

No comments:

Post a Comment