Triple DES
Also referred to as 3DES, a mode of the DES encryption algorithm that encrypts data three times. Three 64-bit keys are used, instead of one, for an overall key length of 192 bits means TripleDES uses three successive iterations of the DES algorithm. It can use either two or three 56-bit keys. This algorithm supports key lengths from 128 bits to 192 bits in increments of 64 bits.
Terms:
- Plaintext- It is the original intelligible message or data that is fed into the algorithm as input.
- Secret key –It is also input to the encryption algorithm. The key is value independent of the plaintext.
- Ciphertext –This is the scrambled message produced as output. It depends on the plaintext and secret key.
- Encryption algorithm- It performs various substitution and transformation on the plaintext and produce ciphertext base on key.
- Decryption algorithm- This is essentially the encryption algorithm run in reverse. It takes the ciphertext and secret key and produces the original plaintext.
DES (Data Encryption standard) is symmetric key encryption technology. Triple DES uses a “key bundle” which comprises three DES keys, K1, K2 and K3, each of 56 bits (excluding parity bits).
The encryption algorithm is:
ciphertext = EK3(DK2(EK1(plaintext)))
I.e., DES encrypt with K1, DES decrypt with K2, then DES encrypt with K3.
Decryption is the reverse:
plaintext = DK1(EK2(DK3(ciphertext)))
I.e., decrypt with K3, encrypt with K2, then decrypt with K1.
Each triple encryption encrypts one block of 64 bits of data.In each case the middle operation is the reverse of the first and last. This improves the strength of the algorithm when using keying option 2, and provides backward compatibility with DES with keying option 3.
Workflow in this example:
- Create a class to encrypt and decrypt plaintext and ciphertext respectively.
- Create a web form which get input secret key and plaintext.
- Web form contains two button for encrypt and decrypt.
In .net System.Security.Cryptography namespace has encrypt and decrypt classes.
Steps:
Create a class for encryption and decryption.
1.Using namespace
using System;
using System.Security.Cryptography;
using System.Text;
2. In this class create a method which return TripleDES object and take secrete key as input. Set key and initialization vector (IV) to tripleDES object
private TripleDES CreateDES(string key)
{
TripleDES tDes = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
tDes.Key = hashmd5.ComputeHash(Encoding.Unicode.GetBytes(key));
tDes.IV = new byte[tDes.BlockSize / 8];
return tDes;
}
3.Create a method for encryption which takes two input parameter plaintext and secret key and return byte(ciphertext).
public byte[] Encryption(string plainText, string key)
{
TripleDES tDes = CreateDES(key);
ICryptoTransform ict = tDes.CreateEncryptor();
byte[] input = Encoding.Unicode.GetBytes(plainText);
return ict.TransformFinalBlock(input, 0, input.Length);
}
4.Create a method for decryption which takes two input parameter ciphertext and secret key and ciphertext and return plaintext.
public string Decryption(string cipherText, string key)
{
byte[] cipherByteText = Convert.FromBase64String(cipherText);
TripleDES tDes = CreateDES(key);
ICryptoTransform ict = tDes.CreateDecryptor();
byte[] output = ict.TransformFinalBlock(cipherByteText, 0, cipherByteText.Length);
return Encoding.Unicode.GetString(output);
}
5.Create a Form which contain two textboxes, two labels and two buttons:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EncDec.aspx.cs" Inherits="EncDec" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Encryption and Decryption</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td> Enter Key </td>
<td> <asp:TextBox ID="txtKey" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Enter Plain Text</td>
<td> <asp:TextBox ID="txtPlainText" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnEncrypt" runat="server" Text="Encryption" OnClick="btnEncrypt_Click" />
</td>
</tr>
<tr>
<td>Encrypted Text</td>
<td><asp:Label ID="lblEncrypted" runat="server"></asp:Label></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnDecrypt" runat="server" Text="Decryption" OnClick="btnDecrypt_Click" />
</td>
</tr>
<tr>
<td>Decrypted Text</td>
<td><asp:Label ID="lblDecrypt" runat="server"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
6.Create button event for encryption and decryption
using System;
public partial class EncDec : System.Web.UI.Page
{
EncryptionDecryption objEncDec = new EncryptionDecryption();
protected void btnEncrypt_Click(object sender, EventArgs e)
{
lblEncrypted.Text = Convert.ToBase64String(objEncDec.Encryption(txtPlainText.Text, txtKey.Text));
}
protected void btnDecrypt_Click(object sender, EventArgs e)
{
lblDecrypt.Text = objEncDec.Decryption(lblEncrypted.Text, txtKey.Text);
}
}
OUTPUT

Encryption and Decryption string with .NET