a developer's notes – a semi-technical web development BLOG

December 17, 2014

TripleDes Encryption/Decryption in C#

Filed under: C# — Duy Nguyen @ 10:15 am
Tags: , , ,
public static class TripleDESUtility
{
    /// <summary>
    /// Encrypt using Triple DES.
    /// </summary>
    public static string EncryptTripleDES(string PlainText)
    {
        TripleDES des = CreateDES();
        ICryptoTransform ct = des.CreateEncryptor();
        byte[] input = Encoding.UTF8.GetBytes(PlainText);
        var output = ct.TransformFinalBlock(input, 0, input.Length);

        return Convert.ToBase64String(output);
    }

    /// <summary>
    /// Decrypt using Triple DES.
    /// </summary>
    public static string DecryptTripleDES(string CypherText)
    {
        byte[] b = Convert.FromBase64String(CypherText);
        TripleDES des = CreateDES();
        ICryptoTransform ct = des.CreateDecryptor();
        byte[] output = ct.TransformFinalBlock(b, 0, b.Length);

        return Encoding.UTF8.GetString(output);
    }

    // This is not used in the TripleDESUtility
    public static string SHA1Hash(string text)
    {
        var temp = Encoding.ASCII.GetBytes(text);
        using (SHA1Managed sha1 = new SHA1Managed())
        {
            var hash = sha1.ComputeHash(temp);

            return Convert.ToBase64String(hash);
        }
    }

    /// <summary>
    /// Create the Tripel DES encryption object instance.
    /// </summary>
    private static TripleDES CreateDES()
    {
        TripleDES des = new TripleDESCryptoServiceProvider();

        // MD5 Hashing Key
        MD5 md5 = new MD5CryptoServiceProvider();
        des.Key = md5.ComputeHash(Encoding.UTF8.GetBytes(GetKeyString()));

        des.Key = TripleDESUtility.GetKeyBytes();
        des.Mode = CipherMode.CBC;
        des.Padding = PaddingMode.PKCS7; // PKCS7 and PKCS5 algorthims are the same.

        des.IV = GetIVBytes();
        return des;
    }
    
    private static byte[] GetKeyBytes()
    {
        string encodedKey = ConfigurationManager.AppSettings["AppKeyThatContainsTheEncryptionKey"];
        
        byte[] data = Convert.FromBase64String(encodedKey);
        return data;
    }

    // Decode a base64 encoded string to a readable string
    private static string GetKeyString()
    {
        string encodedKey = ConfigurationManager.AppSettings["AppKeyThatContainsTheEncryptionKey"];

        byte[] data = Convert.FromBase64String(encodedKey);
        return Encoding.UTF8.GetString(data);
    }

    private static byte[] GetIVBytes()
    {
        string IV = ConfigurationManager.AppSettings["AppKeyThatContainsTheEncryptionKey"];

        return Convert.FromBase64String(IV);
    }
}
Advertisements

2 Comments »

  1. I have a problem with this class. I’m trying to encript some text, with my own key in a constant, but i have an error in line ” des.IV = GetIVBytes();” –> “Specified initialization vector (IV) does not match the block size for this algorithm.”

    can you help me? IV value in this method is the same than encodedkey?

    Comment by fernando — January 9, 2015 @ 1:57 pm | Reply

  2. I have updated the post. Give this a try!

    Comment by adevelopersnotes — January 9, 2015 @ 2:57 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: