TripleDES Encrypt/Decrypt Extension Methods

12. August 2009

Every developer has reusable utilities that they have created and perfected during their time in development. One of the most favorite ones in my arsenal is a utility to encrypt and decrypt strings using TripleDES encryption.

Instead of having to look up the System.Security.Cryptography namespace every time I need to encrypt or decrypt a string, which honestly I only use about once every few months, I have converted my "helper" class to use Extension methods. So instead of:

string val = "something";
string encrypted = RobBihun.Tools.Security.TDESEncryption.Encrypt(val);

I can have:

string val = "something";
string encrypted = val.Encrypt();

Of course, you need to import the namespace that contains the extension methods. Also, you should create your own encryption keys instead of using hard-coded defaults. Here is the full code in case you want to use it.

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace RobBihun.Tools.ExtensionMethods
{
    public static class StringExtensions
    {
        private static TripleDESCryptoServiceProvider tDesProvider = new TripleDESCryptoServiceProvider();
        private static UTF8Encoding strCoder = new UTF8Encoding();

        //encryption keys to use by default - Best practice would be to use custom keys set at runtime (not hard coded)
        private static byte[] badEncKey = { 1, 3, 77, 5, 5, 98, 7, 55, 9, 10, 3, 12, 5, 15, 15, 36, 17, 28, 19, 20, 8, 22, 88, 24 };
        private static byte[] badEncIv = { 8, 7, 6, 5, 4, 3, 2, 1 };

        public static string Encrypt(this string text)
        {
            //throw new Exception("You're an idiot");
            byte[] input = strCoder.GetBytes(text);
            byte[] output = input.Transform(tDesProvider.CreateEncryptor(badEncKey, badEncIv));
            return Convert.ToBase64String(output);
        }

        public static string Decrypt(this string text)
        {
            //throw new Exception("You're a major idiot");
            byte[] input = Convert.FromBase64String(text);
            byte[] output = input.Transform(tDesProvider.CreateDecryptor(badEncKey, badEncIv));
            return strCoder.GetString(output);
        }

        public static string Encrypt(this string text, byte[] encKey, byte[] encIv)
        {
            byte[] input = strCoder.GetBytes(text);
            byte[] output = input.Transform(tDesProvider.CreateEncryptor(encKey, encIv));
            return Convert.ToBase64String(output);
        }

        public static string Decrypt(this string text, byte[] encKey, byte[] encIv)
        {
            byte[] input = Convert.FromBase64String(text);
            byte[] output = input.Transform(tDesProvider.CreateDecryptor(encKey, encIv));
            return strCoder.GetString(output);
        }


        private static byte[] Transform(this byte[] input, ICryptoTransform CryptoTransform)
        {
            MemoryStream memStream = new MemoryStream();

            CryptoStream cryStream = new CryptoStream(memStream, CryptoTransform, CryptoStreamMode.Write);
            cryStream.Write(input, 0, input.Length);
            cryStream.FlushFinalBlock();
            memStream.Position = 0;

            byte[] result = memStream.ToArray();

            memStream.Close();
            cryStream.Close();

            return result;
        }
    }
}

Of course, if you have any comments or suggestions, please leave them below!

General ,

Comments are closed