Triple DES Encryption and Decryption using User provided key

No.of Views2013
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : CSharp , Cryptography
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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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

Image Loading

 

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
The 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;
        }
    }
}

Program Output

Image Loading

Conclusion

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

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
By:RRaveenDate Of Posted:11/13/2010 8:24:03 PM
Please send source file as zip
Hi, Please provide this article source code. becuase of readers does not understand from the code which you have given here.
By:HikoDate Of Posted:11/13/2010 12:56:34 PM
Help me..
I don't understand it...(( Please send me Full function(whole(all) form), for visual basic 2008....
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top