TripleDES Encryption In C# And .NET Core



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.

using System; 
using System.IO; 
using System.Security.Cryptography; 
class TripleDESExample
    public static void Main() { 
        Console.WriteLine("Enter data that needs to be encrypted..."); 
        string data = Console.ReadLine(); 
        Apply3DES(data); 
        Console.ReadLine(); 
    } 
    static void Apply3DES(string raw) { 
        try
            // Create 3DES that generates a new key and initialization vector (IV). 
            // Same key must be used in encryption and decryption 
            using(TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider()) { 
                // Encrypt string 
                byte[] encrypted = Encrypt(raw, tdes.Key, tdes.IV); 
                // Print encrypted string 
                Console.WriteLine("Encrypted data:" + System.Text.Encoding.UTF8.GetString(encrypted)); 
                // Decrypt the bytes to a string. 
                string decrypted = Decrypt(encrypted, tdes.Key, tdes.IV); 
                // Print decrypted string. It should be same as raw data 
                Console.WriteLine("Decrypted data: " + decrypted); 
            } 
        } catch (Exception exp) { 
            Console.WriteLine(exp.Message); 
        } 
        Console.ReadKey(); 
    } 
    static byte[] Encrypt(string plainText, byte[] Key, byte[] IV) { 
        byte[] encrypted; 
        // Create a new TripleDESCryptoServiceProvider. 
        using(TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider()) { 
            // Create encryptor 
            ICryptoTransform encryptor = tdes.CreateEncryptor(Key, IV); 
            // Create MemoryStream 
            using(MemoryStream ms = new MemoryStream()) { 
                // Create crypto stream using the CryptoStream class. This class is the key to encryption 
                // and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream 
                // to encrypt 
                using(CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { 
                    // Create StreamWriter and write data to a stream 
                    using(StreamWriter sw = new StreamWriter(cs)) 
                    sw.Write(plainText); 
                    encrypted = ms.ToArray(); 
                } 
            } 
        } 
        // Return encrypted data 
        return encrypted; 
    } 
    static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV) { 
        string plaintext = null
        // Create TripleDESCryptoServiceProvider 
        using(TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider()) { 
            // Create a decryptor 
            ICryptoTransform decryptor = tdes.CreateDecryptor(Key, IV); 
            // Create the streams used for decryption. 
            using(MemoryStream ms = new MemoryStream(cipherText)) { 
                // Create crypto stream 
                using(CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) { 
                    // Read crypto stream 
                    using(StreamReader reader = new StreamReader(cs)) 
                    plaintext = reader.ReadToEnd(); 
                } 
            } 
        } 
        return plaintext; 
    } 
    private static void EncryptFile(String inName, String outName, byte[] desKey, byte[] desIV) { 
        //Create the file streams to handle the input and output files. 
        FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); 
        FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); 
        fout.SetLength(0); 
        //Create variables to help with read and write. 
        byte[] bin = new byte[100]; //This is intermediate storage for the encryption. 
        long rdlen = 0; //This is the total number of bytes written. 
        long totlen = fin.Length; //This is the total length of the input file. 
        int len; //This is the number of bytes to be written at a time. 
        DES des = new DESCryptoServiceProvider(); 
        CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write); 
        Console.WriteLine("Encrypting..."); 
        //Read from the input file, then encrypt and write to the output file. 
        while (rdlen < totlen) { 
            len = fin.Read(bin, 0, 100); 
            encStream.Write(bin, 0, len); 
            rdlen = rdlen + len; 
            Console.WriteLine("{0} bytes processed", rdlen); 
        } 
        encStream.Close(); 
        fout.Close(); 
        fin.Close(); 
    } 
}  

Output is like following