SIGN UP MEMBER LOGIN:    
ARTICLE

Triple DES Encryption and Decryption using User provided key

Posted by Dhananjay Kumar Articles | Cryptography C# October 29, 2009
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.
Reader Level:


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.

Login to add your contents and source code to this article
share this article :
post comment
 

Hello Dhananjay i translated your code to vb.net and everything is working fine, except for 4 strange characters that appear in the decrypted textbox. Can you please help me with this issue??

Posted by Manuel Martinez Apr 01, 2011

thanks for share this code

Posted by Nam Na Jan 25, 2011

thanx for the code... so easy to understand.. actually hw to execte this.. normal compilation of code in C  or any oher plss specify....,, i need to do for mini project


Posted by ARUN KUMAR Nov 27, 2010

thanks for sharing the code, very interesting!

----------------------------
Danny, San Jose Locksmith

Posted by danny dror Nov 23, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor