Click here to Skip to main content
15,881,752 members
Articles / Web Development / ASP.NET
Tip/Trick

Encryption/Decryption Function in .NET using the TripleDESCryptoServiceProvider Class

Rate me:
Please Sign up or sign in to vote.
4.89/5 (26 votes)
16 Jan 2012CPOL 165.7K   30   15
String encryption/decryption functions in .NET.

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:


C#
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:

C#
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:

C#
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)


Written By
Software Developer (Senior)
India India
I have around 10 Years of experience using Microsoft technologies. I started working on microsoft technologies from Visual Studio 6.0 to VS 2015. I have worked on both Windows and Web applications development. I have experience of using languages and technologies such as C#, VB.Net, ASP.Net, MVC, WPF, WCF, XML Web Services, Java Script, JQuery and databases such as SQL Server, Oracle, Sybase and DB2, Also Markup languages like XML, HTML and XAML.

In my leisure time I like to watch movies, play video games and read about trending technologies.

Comments and Discussions

 
Questionencryption in mvc Pin
Member 134311833-Oct-17 18:45
Member 134311833-Oct-17 18:45 
GeneralMy vote of 1 Pin
1337Architect5-Oct-14 10:49
professional1337Architect5-Oct-14 10:49 
QuestionWorks like a charm Pin
PiyushVarma29-Aug-14 11:38
PiyushVarma29-Aug-14 11:38 
GeneralMy vote of 1 Pin
1337Architect22-Aug-14 19:18
professional1337Architect22-Aug-14 19:18 
GeneralMy vote of 5 Pin
vajihe-ha7-Jul-14 9:50
vajihe-ha7-Jul-14 9:50 
QuestionRestrict size of encrypted string Pin
niravsahayata17-Apr-13 4:55
niravsahayata17-Apr-13 4:55 
Questiongreat piece of code Pin
abhattacharjee29-May-12 6:58
abhattacharjee29-May-12 6:58 
BugI get a Bug,what is the use of "passphrase" Pin
Govardhan Rao Ganji16-May-12 21:19
Govardhan Rao Ganji16-May-12 21:19 
GeneralRe: I get a Bug,what is the use of "passphrase" Pin
B. Clay Shannon24-Oct-12 14:22
professionalB. Clay Shannon24-Oct-12 14:22 
Generalmy friend sumit in your code function for encryption is work... Pin
Member 80248903-Jan-12 10:00
Member 80248903-Jan-12 10:00 
GeneralDo the changes [corrections,.. etc] asked by lithron comment... Pin
Member 43208443-Jan-12 7:33
Member 43208443-Jan-12 7:33 
GeneralReason for my vote of 1 You don't encrypt with MD5. Please ... Pin
lithron27-Dec-11 12:09
lithron27-Dec-11 12:09 
GeneralRe: Thanks for the inputs lithron. Will make it more informative... Pin
kadu.sumit27-Dec-11 19:47
kadu.sumit27-Dec-11 19:47 
General[My vote of 1] Horrible cryptography Pin
Member 28577353-Jan-12 8:04
Member 28577353-Jan-12 8:04 
GeneralRe: [My vote of 1] Horrible cryptography Pin
kadu.sumit5-Jan-12 2:49
kadu.sumit5-Jan-12 2:49 

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

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