C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Encryption and Decryption in C#


Posted Date: 29 Oct 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: Nikhil GaurMember Level: Diamond    
Rating: 1 out of 5Points: 10



Many time we need to encrypt the messages in out application for a high level of security..

here is the code for doing so. We just need to include the name space System.Security in our application. In this code we can use the Key to encrypt and Decrypt, thus it will provide a higher level of security. That key we can also save in the Web config file or can provide from front end.

here is the code.

#region "Encryption & Decryption"

public static string Encrypt(stringtoEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray =UTF8Encoding.UTF8.GetBytes(toEncrypt);
string key = "543z";

if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = newMD5CryptoServiceProvider();
keyArray =hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray =UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes =new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform =tdes.CreateEncryptor();
byte[] resultArray =cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
returnConvert.ToBase64String(resultArray, 0, resultArray.Length);
}

public static string Decrypt(stringcipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray =Convert.FromBase64String(cipherString);
string key = "543z";

if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = newMD5CryptoServiceProvider();
keyArray =hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray =UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes =new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform =tdes.CreateDecryptor();
byte[] resultArray =cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

tdes.Clear();
returnUTF8Encoding.UTF8.GetString(resultArray);
}

#endregion


For more details, visit http://niksgaur.blogspot.com/2009/07/code-encryption-and-decryption-using.html



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Security  .  Encryption  .  Decryption  .  .net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Application to decompress file in asp.net
Previous Resource: Changing Dynamically allocate Title, Meta Keywords and Meta description in to a Page
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use