In this article we will see how to encrypt the messages and again decrypt the encrypted messages. Those days we need more sequrity for hiding our personal messages; .Net provides various way to encrypt and again decrypt those messages.
For this purpose I used the "MD5CryptoServiceProvider" and "TripleDESCryptoServiceProvider" to encrypt our messages. We can easilly use those hash providers and convert our messages to understandable format i.e. some encrypted format.
Generally this encryption and decryption is very usefull in storing username and password in encrypted format.
Because somebody has access to your server and he can see the user information, to restrict such misuse of data while storing, we can store the data in encrypted format. After storing encrypted usernames and password while checking the user you can compare each field.
Using Code:
Now we will move to how we can encrypt the message. In my Utility class EncryptyString Method will encrypt the string which will take the message to encrypt and one passphrase as argument and return the encrypted string.
public static string EncryptString(string Message, string Passphrase)
{
byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
// Step 1. We hash the passphrase using MD5
// We use the MD5 hash generator as the result is a 128 bit byte array
// which is a valid length for the TripleDES encoder we use below
MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
// Step 2. Create a new TripleDESCryptoServiceProvider object
TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
// Step 3. Setup the encoder
TDESAlgorithm.Key = TDESKey;
TDESAlgorithm.Mode = CipherMode.ECB;
TDESAlgorithm.Padding = PaddingMode.PKCS7;
// Step 4. Convert the input string to a byte[]
byte[] DataToEncrypt = UTF8.GetBytes(Message);
// Step 5. Attempt to encrypt the string
try
{
ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
}
finally
{
// Clear the TripleDes and Hashprovider services of any sensitive information
TDESAlgorithm.Clear();
HashProvider.Clear();
}
// Step 6. Return the encrypted string as a base64 encoded string
return Convert.ToBase64String(Results);
}
In the same manner you can decrypt this encrypted string using DecryptString Method which will take decrypted string and passphrase as argument and return the plain message i.e. decrypted message.
public static string DecryptString(string Message, string Passphrase)
{
byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
// Step 1. We hash the passphrase using MD5
// We use the MD5 hash generator as the result is a 128 bit byte array
// which is a valid length for the TripleDES encoder we use below
MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
// Step 2. Create a new TripleDESCryptoServiceProvider object
TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
// Step 3. Setup the decoder
TDESAlgorithm.Key = TDESKey;
TDESAlgorithm.Mode = CipherMode.ECB;
TDESAlgorithm.Padding = PaddingMode.PKCS7;
// Step 4. Convert the input string to a byte[]
byte[] DataToDecrypt = Convert.FromBase64String(Message);
// Step 5. Attempt to decrypt the string
try
{
ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
}
finally
{
// Clear the TripleDes and Hashprovider services of any sensitive information
TDESAlgorithm.Clear();
HashProvider.Clear();
}
// Step 6. Return the decrypted string in UTF8 format
return UTF8.GetString(Results);
}
Conclusion:
Using those hash provider classes of .Net you can encrypt and again decrypt the messages.

Kuppurasu NagarajPosted May 6, 2016, 9:13 AM
Nice Sharing..
Zeeshan AzimPosted Feb 21, 2015, 2:13 PM
Thanks Mate ! it is very helpful for me.
Anoop ShrivastavaPosted Feb 25, 2014, 1:37 AM
Above Given sample works fine, Thanks alot.....
Saineshwar BageriPosted Jan 8, 2014, 11:23 AM
algorithm name
Krishna GaradPosted Jan 8, 2014, 6:37 AM
@Saineshwar make sure Passphrase passed while encrypting is same while decrypting.
Saineshwar BageriPosted Jan 5, 2014, 11:46 PM
which alogrithm it is
Saineshwar BageriPosted Jan 5, 2014, 11:36 PM
but MD5 is not able to decrypt
anil babuPosted Aug 15, 2012, 4:18 AM
Tell me how to perform all DML operations in WINDOWS azure? plz tell me i am new this one Help me
Veeramanikandan DhanabalanPosted Mar 16, 2011, 3:54 AM
Hi.. This article is superb... the encryption portion is works fine... but the decryption portion always shows like this meassage... Invalid length for a Base-64 char array. So kindly tell me.. how to give the input string in Encrypt message text