KeyLimeTie Blog

Simple 3DES Encryption

By Brian Pautsch – 3/7/2006 8:40:43 PM. Posted to Code Snippets.

Looking for a simple class to encrypt/decrypt strings such as passwords? Use this one...it's about as easy as it gets. And be sure to add a plain text phrase in your web.config (key="EncryptionKey") or hard code it in the app to make it impossible to find.

1using System;
2using System.Configuration;
3using System.Security.Cryptography;
4using System.Text;
5
6namespace com.BrianPautsch
7{
8 public class Cryptology
9 {
10  #region Encrypt
11  public static string Encrypt(string strPlainText)
12  {
13   TripleDESCryptoServiceProvider objDES = 
new TripleDESCryptoServiceProvider(); 14 MD5CryptoServiceProvider objMD5 =
new MD5CryptoServiceProvider(); 15 string strKey =
ConfigurationSettings.AppSettings["EncryptionKey"].ToString(); 16 objDES.Key = objMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strKey)); 17 objDES.Mode = CipherMode.ECB; 18 ICryptoTransform objDESEncrypt = objDES.CreateEncryptor(); 19 byte[] arrBuffer = ASCIIEncoding.ASCII.GetBytes(strPlainText); 20 return Convert.ToBase64String(objDESEncrypt.TransformFinalBlock(
arrBuffer, 0, arrBuffer.Length)); 21 } 22 #endregion 23 #region Decrypt 24 public static string Decrypt(string strBase64Text) 25 { 26 TripleDESCryptoServiceProvider objDES =
new TripleDESCryptoServiceProvider(); 27 MD5CryptoServiceProvider objMD5 =
new MD5CryptoServiceProvider(); 28 string strKey =
ConfigurationSettings.AppSettings["EncryptionKey"].ToString(); 29 objDES.Key = objMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strKey)); 30 objDES.Mode = CipherMode.ECB; 31 ICryptoTransform objDESEncrypt = objDES.CreateDecryptor(); 32 byte[] arrBuffer = Convert.FromBase64String(strBase64Text); 33 return ASCIIEncoding.ASCII.GetString(objDESEncrypt.TransformFinalBlock(
arrBuffer, 0, arrBuffer.Length)); 34 } 35 #endregion 36 } 37}

Comments

On 1/8/2010 Sathyaish Chakravarthy said:
Brian,

On 5/28/2006 Distantkiwi said:
Thanks for the simplicity

On 5/15/2006 Stuart Dawson said:
Superb. Thank you for keeping it simple Brian!

On 5/13/2006 bobmoff said:
great work. simple and good.

On 4/25/2006 Brian Pautsch said:
Yeah...most of the examples out there are unnecessarily complicated.

On 4/25/2006 Björn Hallin said:
Thanks Brian, I had spent some time searching for a simple solution to this problem :)

On 3/18/2006 Kris said:
Thanks Brian, I was looking for a much simpler way of doing this. Thanks a bunch.

Leave a Comment

Name:
Email:
URL:
Comment:
Security Code:
Type Security Code:

Photos on Flickr

More Photos »

Search Blog


Get Email Updates

Like what you read here at KeyLimeTie? Sign up for our email list!

Subscribe