Click here to Skip to main content
Click here to Skip to main content

Encryption/Decryption Function in .NET using the TripleDESCryptoServiceProvider Class

By , 16 Jan 2012
Rate this:
Please Sign up or sign in to vote.

I would like to share some simple functions which can encrypt and decrypt strings. These functions can be used in any of .NET Framework supporting languages.

Just declare a pass phrase in your code like a master key which allows the MD5CryptoServiceProvider class (in the System.Security and System.Security.Cryptography namespace) to compute a hash value for encryption/decryption. TripleDESCryptoServiceProvider class is used to encrypt/ decrypt strings which in turn uses 3DES (Triple Data Encryption Standard) algorithm. 3DES alogorithm uses three 64-bit long keys to (Encrypt-Decrypt-Encrypt) data.
 
Declare the pass phrase as below, and you can set any string value you like:

const string passphrase = "password";

For example, "password" is the key I used here.

Now, you just have to use the Encrypt/Decrypt functions below in your class to encrypt and decrypt any string.

Below are the functions:

Encrypt function:
public static string EncryptData(string Message)
{
    byte[] Results;
    System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
    MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
    byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase));
    TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
    TDESAlgorithm.Key = TDESKey;
    TDESAlgorithm.Mode = CipherMode.ECB;
    TDESAlgorithm.Padding = PaddingMode.PKCS7;
    byte[] DataToEncrypt = UTF8.GetBytes(Message);
    try
    {
        ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
        Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
    }
    finally
    {                
        TDESAlgorithm.Clear();
        HashProvider.Clear();
    }
    return Convert.ToBase64String(Results);
}
Decrypt function:
public static string DecryptString(string Message)
{
    byte[] Results;
    System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
    MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
    byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase));
    TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
    TDESAlgorithm.Key = TDESKey;
    TDESAlgorithm.Mode = CipherMode.ECB;
    TDESAlgorithm.Padding = PaddingMode.PKCS7;
    byte[] DataToDecrypt = Convert.FromBase64String(Message);
    try
    {
        ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
        Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
    }
    finally
    {
        TDESAlgorithm.Clear();
        HashProvider.Clear();
    }
    return UTF8.GetString(Results);
}

Hope this helps someone save time while coding.

Happy coding!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

kadu.sumit
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
QuestionRestrict size of encrypted string Pinmemberniravsahayata17-Apr-13 4:55 
Questiongreat piece of code Pinmemberabhattacharjee29-May-12 6:58 
BugI get a Bug,what is the use of "passphrase" PinmemberGovardhan Rao Ganji16-May-12 21:19 
GeneralRe: I get a Bug,what is the use of "passphrase" PinmemberMember 904013724-Oct-12 14:22 
Generalmy friend sumit in your code function for encryption is work... PinmemberMember 80248903-Jan-12 10:00 
GeneralDo the changes [corrections,.. etc] asked by lithron comment... PinmemberMember 43208443-Jan-12 7:33 
GeneralReason for my vote of 1 You don't encrypt with MD5. Please ... Pinmemberlithron27-Dec-11 12:09 
GeneralRe: Thanks for the inputs lithron. Will make it more informative... Pinmemberkadu.sumit27-Dec-11 19:47 
General[My vote of 1] Horrible cryptography [modified] PinmemberMember 28577353-Jan-12 8:04 
GeneralRe: [My vote of 1] Horrible cryptography Pinmemberkadu.sumit5-Jan-12 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 16 Jan 2012
Article Copyright 2011 by kadu.sumit
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid