In this post, a simple demonstration of how TripleDES Encryption algorithm can be implemented using System.Security.Cryptography namespace in .NET will be explained.
First of all, we need to create a XML file where we will be storing our Special Key. This Key will be used for both Encryption and Decryption not like RSA. DES is symmetric key encryption algorithm.
This special Key will be generated using MD5, which is a cryptographic hash function which takes arbitrary block of data and returns a Stream of Bits of Fixed Length.
Create a Simple Web Application and create a New Class with the name “TripleDESCrypt”.
Assume we have TripleDESKey.xml in our same Application Folder. Lets create a method to update the path variable from Aspx page. Include the following in “TripleDESCrypt“ class.
public void updateXmlPath(string xmlPath)
{
myPath = xmlPath;
}
Now, include the below method in our “TripleDESCrypt“ class. This method will be used to create 16-Byte Key using MD5 and this Special Key will be saved in our TripleDESKey.xml file.
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] hashKey = md5.ComputeHash(Encoding.Unicode.GetBytes(word));
string sss = Convert.ToBase64String(hashKey);
XElement ele = new XElement(“key”, sss);
ele.Save(myPath);
}
To implement this, include the below lines of code in Page Load method of Aspx Page. Below the Secret Key that the user inputs will be converted to MD5 HashKey and will be saved in the XML file.
my3DES.updateXmlPath(Server.MapPath(“TripleDESKey.xml”));
my3DES.GenerateMD5HashKey(“Your Secret Key”);