dcsimg
 

Triple DES Encryption and Decryption in C#

Monday Sep 17th 2018 by Tapas Pal

Learn about the TripleDES class, a triple-level encryption and decryption system available in .NET.

Introduction

The Triple Data Encryption Standard (DES) is a symmetric key encryption algorithm for computerized cryptography. As per the algorithm, the same key is used for encryption and decryption. Also, the same block cipher algorithms are applied three times to each data block. To implement TripleDES, .NET provides a TripleDES class present in the System.Security.Cryptography namespace. The TripleDES class represents the base class for .NET Triple Data Encryption Standard algorithms, from which all TripleDES implementations are derived.

Why TripleDES?

TripleDES takes three 64-bit keys, for an overall key length of 192 bits. The procedure for encryption is exactly the same as regular DES, but it is repeated three times, hence the name Triple DES. The data is encrypted with the first key, decrypted with the second key, and finally encrypted again with the third key.

TripleDES is a widely used encryption algorithm. It's predecessor, DES, is inherently insecure, whereas TripleDES has much better security characteristics.

Encryption and Decryption in C Sharp Using TripleDES

To create a Sample Console application to demonstrate Encryption and Decryption application in .NET, Open Visual Studio 2015 -> File Menu -> New, and then Project. It will open a new project window. Choose the Console Application type. Specify the project name "TripleDES" and click OK.

Now, add a new class and name it ClsTripleDES.cs. Add a static function named 'Encrypt'. This function will create a TripleDES string instance. This is taking a string as a key value and it will calculate an MD5 hash on TripleDES classinput parameter string. This hash value would be used as a real key for the encryption.

Similarly, for decryption, add TripleDES class'Decrypt' function. This function will take a CypherText to decrypt. It will return a string value.

Refer to the following code snippet.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace TripleDES
{
   public class ClsTripleDES
   {

      private const string mysecurityKey = "MyTestSampleKey";

      public static string Encrypt(string TextToEncrypt)
      {
         byte[] MyEncryptedArray = UTF8Encoding.UTF8
            .GetBytes(TextToEncrypt);

         MD5CryptoServiceProvider MyMD5CryptoService = new
            MD5CryptoServiceProvider();

         byte[] MysecurityKeyArray = MyMD5CryptoService.ComputeHash
            (UTF8Encoding.UTF8.GetBytes(mysecurityKey));

         MyMD5CryptoService.Clear();

         var MyTripleDESCryptoService = new
            TripleDESCryptoServiceProvider();

         MyTripleDESCryptoService.Key = MysecurityKeyArray;

         MyTripleDESCryptoService.Mode = CipherMode.ECB;

         MyTripleDESCryptoService.Padding = PaddingMode.PKCS7;

         var MyCrytpoTransform = MyTripleDESCryptoService
            .CreateEncryptor();

         byte[] MyresultArray = MyCrytpoTransform
            .TransformFinalBlock(MyEncryptedArray, 0,
            MyEncryptedArray.Length);

         MyTripleDESCryptoService.Clear();

         return Convert.ToBase64String(MyresultArray, 0,
            MyresultArray.Length);
      }



      public static string Decrypt(string TextToDecrypt)
      {
         byte[] MyDecryptArray = Convert.FromBase64String
            (TextToDecrypt);

         MD5CryptoServiceProvider MyMD5CryptoService = new
            MD5CryptoServiceProvider();

         byte[] MysecurityKeyArray = MyMD5CryptoService.ComputeHash
            (UTF8Encoding.UTF8.GetBytes(mysecurityKey));

         MyMD5CryptoService.Clear();

         var MyTripleDESCryptoService = new
            TripleDESCryptoServiceProvider();

         MyTripleDESCryptoService.Key = MysecurityKeyArray;

         MyTripleDESCryptoService.Mode = CipherMode.ECB;

         MyTripleDESCryptoService.Padding = PaddingMode.PKCS7;

         var MyCrytpoTransform = MyTripleDESCryptoService
            .CreateDecryptor();

         byte[] MyresultArray = MyCrytpoTransform
            .TransformFinalBlock(MyDecryptArray, 0,
            MyDecryptArray.Length);

         MyTripleDESCryptoService.Clear();

         return UTF8Encoding.UTF8.GetString(MyresultArray);
      }
   }
}

Next, in the Main () method, add the following code snippet. It will call the previously created Encrypt method with sample text and print it after encryption. Again, after decryption, the decrypted value will be printed on the console.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TripleDES
{
   class Program
   {
      static void Main(string[] args)
      {
         var text = "My Sample Text to Test DES from C#";
         var encryptedText = ClsTripleDES.Encrypt(text);
         var decryptedText = ClsTripleDES.Decrypt(encryptedText);
         Console.WriteLine("Before Encryption Text = " + text);
         Console.WriteLine("After Encryption Text = " +
            encryptedText);
         Console.WriteLine("After Decryption Text = " +
            decryptedText);
         Console.ReadLine();
      }
   }
}

Conclusion

Any encryption algorithm can be broken. It's only a matter of time until 3DES is too broken to be considered secure. Experts says, 3DES is an old algorithm which has many known loopholes, such as slowness, vulnerability, and so forth. Most developers are adopting AES nowadays.

That's all for today. Happy coding!

Home
Mobile Site | Full Site