Triple DES Encryption and Decryption using User provided 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.

Up Next
    Ebook Download
    View all
    Learn
    View all