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 » Encryption »

Encrypting and Decripting values using Security Key


Posted Date: 12 Jun 2009    Resource Type: Code Snippets    Category: Encryption
Author: DotNetMatrixMember Level: Gold    
Rating: 1 out of 5Points: 10



This junk of code is used to Encrypt and Decrypt the values depending on a key called Security Key. Which is saved in the web.config file or in database.


public sealed class EncryptCard
{
private EncryptCard()
{
}


public static string Encrypt(string toEncrypt, bool useHashing)
{
if (!string.IsNullOrEmpty(toEncrypt))
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

AppSettingsReader settingsReader = new AppSettingsReader();


string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
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();

return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
else
{
return toEncrypt = null;
}
}


public static string Decrypt(string decryptValue, bool useHashing)
{
if (!string.IsNullOrEmpty(decryptValue))
{
byte[] keyArray;


byte[] toEncryptArray = Convert.FromBase64String(decryptValue);

// retrieving security key from the web.config file
AppSettingsReader settingsReader = new AppSettingsReader();

string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

if (useHashing)
{

MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));


hashmd5.Clear();
}
else
{

keyArray = UTF8Encoding.UTF8.GetBytes(key);
}

TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();

tdesProvider.Key = keyArray;

tdesProvider.Mode = CipherMode.ECB;

tdesProvider.Padding = PaddingMode.PKCS7;

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

tdesProvider.Clear();

return UTF8Encoding.UTF8.GetString(resultArray);
}
else
{
return decryptValue = null;
}
}
}

Here. I added the securityKey in web.config file

< appSettings >

< add key ="SecurityKey" value="DOTNETMATRIX"/>
</appSettings >



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.
Encription  .  

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: Encrypt/Decrypt text files using Rijndael or TripleDES
Previous Resource: Simple String Encryption and Decryption in .net(C# and VB.NET)
Return to Discussion Resource Index
Post New Resource
Category: Encryption


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use