.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Triple DES Encryption and Decryption using unique key

Posted By:Dhananjay Kumar       Posted Date: December 20, 2009    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  

Triple DES Encryption and Decryption using unique key. In this article, I will explain how to do a Triple DES encryption on a plain text using user provided key. I will calculate a MD5 Hash on the key provided by the user. And that key will be user to encrypt and decrypt the message.
 

Triple DES Encryption and Decryption using unique key

Objective:

In this article, I will explain how to do a Triple DES encryption on a plain text using user provided key.  I will calculate a MD5 Hash on the key provided by the user.  And that key will be user to encrypt and decrypt the message.

Explanation of DES

DES is a symmetric key encryption algorithm.  Same key is being used for encryption and decryption.  So challenge in using symmetric key algorithm is that we need to have the same key for decryption which is used for encryption. People follow different approach to save key. Either they append key with cryptic text or physically save it somewhere.  I am going to ask user to input some string as key. I will calculate MD5 hash on that string input by user to make key. Then I will use this key to encrypt and decrypt the plain text.

Working 

  1. User will enter the key
  2. User will enter the plain text
  3. When User will click the Encrypt button, plain text will get encrypted and display in textbox2.
  4. When user will click on Decrypt button in textbox3 plain text will get display.
Screen 

fig1.gif

Functions :

Function to create DES

This function will create TripleDES instance.  This is taking a string as key value and will calculate MD5 hash on input parameter string.  This hash value would be used  as real key for the encryption. 

static TripleDES CreateDES(string key)
{
  MD5 md5 = new MD5CryptoServiceProvider();
  TripleDES des = new TripleDESCryptoServiceProvider();
  des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));
  des.IV = new byte[des.BlockSize / 8];
  return des;
}


Function to Encrypt 
  1. This function is taking Plain text to encrypt and key
  2. This function is returning a byte array
  3. As parameter for CreateDES , I am passing the key.
public static byte[] Encryption(string PlainText,string key)
{
  TripleDES des = CreateDES(key);
  ICryptoTransform ct = des.CreateEncryptor();
  byte[] input = Encoding.Unicode.GetBytes(PlainText);
  return ct.TransformFinalBlock(input, 0, input.Length);
}


Function to Decrypt 
  1. This function is taking key and CypherText to encrypt.
  2. It is returning a string.
  3. It is creating TripleDES on given key.
public static string Decryption(string CypherText,string key)
{
  byte[] b = Convert.FromBase64String(CypherText);
  TripleDES des = CreateDES(key);
  ICryptoTransform ct = des.CreateDecryptor(); 
  byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
  return Encoding.Unicode.GetString(output);
} 

Full Code

Below is the full code for encryption and decryption. There are two button click events on which we are performing the action.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;  
namespace Encryptionusing_Des 

{
    public partial class Form1 : Form
    {      

        public Form1() 

        {
            InitializeComponent();           

        }  

        private void Encrypt_Click(object sender, EventArgs e) 

        {            

            byte[] buffer = Encryption(textBox1.Text,txtKey.Text); 

            string b = Convert.ToBase64String(buffer);           

            textBox2.Text = b;           

        }  

        public static byte[] Encryption(string PlainText,string key) 

        {  

            TripleDES des = CreateDES(key);  

            ICryptoTransform ct = des.CreateEncryptor();  

            byte[] input = Encoding.Unicode.GetBytes(PlainText);  

            return ct.TransformFinalBlock(input, 0, input.Length);  

        }        

        public static string Decryption(string CypherText,string key) 

        { 

            byte[] b = Convert.FromBase64String(CypherText);  

            TripleDES des = CreateDES(key);
            ICryptoTransform ct = des.CreateDecryptor();  
            byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
            return Encoding.Unicode.GetString(output);  

        }  

        private void Decrypt_Click(object sender, EventArgs e) 

        {  

            textBox3.Text = Decryption(textBox2.Text,txtKey.Text);  

        }  

        static TripleDES CreateDES(string key) 

        { 
            MD5 md5 = new MD5CryptoServiceProvider();
            TripleDES des = new TripleDESCryptoServiceProvider();
            des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));
            des.IV = new byte[des.BlockSize / 8];
            return des;
        }
    }
}
Output

fig2.gif

Conclusion

I discussed how to encrypt and decrypt a text using user provided key. Thanks for reading.

 Subscribe to Articles

     

Further Readings:

Responses
Author: Nish         Company URL: http://www.dotnetspark.com
Posted Date: October 22, 2010

Hi
It is really a nice article,
can you help me to decode some values.

<PasswordHash>14F009DDBEF203EDC2124D54FA6D325E16E36D77</PasswordHash>
<SaltKey>vw7zHZI=</SaltKey>

This is in TripleDes encryption with the key is 1234567890123451

Thanks
Nish

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend