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 » Articles » .NET Framework »

How to secure data using cryptography


Posted Date: 29 Mar 2005    Resource Type: Articles    Category: .NET Framework
Author: Yogesh S BagulMember Level: Bronze    
Rating: 1 out of 5Points: 20



Introduction


I used symmetric key algorithm to secure the data. so the same key is used for
encryption and decryption of data.
in following class i used SHA1 algorithm,but u can also use MD5 algorithm by setting
string t_hashAlgorithm ="MD5"
For Extra security u can use salt.

u can set different parameters by setting the following properties of algorithm.


string t_salt = "yogesh"; // can be any string
string t_hashAlgorithm = "SHA1"; // can be "MD5"
int t_passwordIterations = 2; // can be any number
string t_initVector = "51cgc5D4e5F6gbc43"; // must be 16 bytes
int t_keySize = 256; // can be 192 or 128

I Have Given a NewSecurity Class below.
It has two stratic functions as below
1. public static string Encrypt(string p_plainText,string p_passPhrase )
2. public static string Decrypt(string p_cipherText, string p_passPhrase )




NewSecurity Class





public class NewSecurity
{
//encrypt fun takes the string to be encrypted and passward as a parameter and
return encrypted string
public static string Encrypt(string p_plainText,
string p_passPhrase )
{
string t_salt = "yogesh"; // can be any string
string t_hashAlgorithm = "SHA1"; // can be "MD5"
int t_passwordIterations = 2; // can be any number
string t_initVector = "51cgc5D4e5F6gbc43"; // must be 16 bytes
int t_keySize = 256; // can be 192 or 128



byte[] initVectorBytes = Encoding.ASCII.GetBytes(t_initVector);
byte[] saltBytes = Encoding.ASCII.GetBytes(t_salt);


byte[] plainTextBytes = Encoding.UTF8.GetBytes(p_plainText);

PasswordDeriveBytes password = new PasswordDeriveBytes(
p_passPhrase,
saltBytes,
t_hashAlgorithm,
t_passwordIterations);

byte[] keyBytes = password.GetBytes(t_keySize / 8);

RijndaelManaged symmetricKey = new RijndaelManaged();

symmetricKey.Mode = CipherMode.CBC;



ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
keyBytes,
initVectorBytes);

MemoryStream memoryStream = new MemoryStream();

CryptoStream cryptoStream = new CryptoStream(memoryStream,
encryptor,
CryptoStreamMode.Write);

cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);


cryptoStream.FlushFinalBlock();


byte[] cipherTextBytes = memoryStream.ToArray();


memoryStream.Close();
cryptoStream.Close();


string t_cipherText = Convert.ToBase64String(cipherTextBytes);


return t_cipherText;
}


public static string Decrypt(string p_cipherText, string p_passPhrase )
{

string t_salt = "yogesh";
string t_hashAlgorithm = "SHA1";
int t_passwordIterations = 2;
string t_initVector = "51cgc5D4e5F6gbc43";
int t_keySize = 256;


byte[] initVectorBytes = Encoding.ASCII.GetBytes(t_initVector);
byte[] saltBytes = Encoding.ASCII.GetBytes(t_salt);


byte[] cipherTextBytes = Convert.FromBase64String(p_cipherText);

PasswordDeriveBytes password = new PasswordDeriveBytes(
p_passPhrase,
saltBytes,
t_hashAlgorithm, t_passwordIterations);


byte[] keyBytes = password.GetBytes(t_keySize / 8);

RijndaelManaged symmetricKey = new RijndaelManaged();


symmetricKey.Mode = CipherMode.CBC;


ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
keyBytes,
initVectorBytes);

MemoryStream memoryStream = new MemoryStream(cipherTextBytes);


CryptoStream cryptoStream = new CryptoStream(memoryStream,
decryptor,
CryptoStreamMode.Read);


byte[] plainTextBytes = new byte[cipherTextBytes.Length];


int decryptedByteCount = cryptoStream.Read(plainTextBytes,
0,
plainTextBytes.Length);

memoryStream.Close();
cryptoStream.Close();


string t_plainText = Encoding.UTF8.GetString(plainTextBytes,
0,
decryptedByteCount);


return t_plainText;
}
}




Summary


IN THIS WAY U CAN SECURE UR DATA BY ENCRYPTING IT ......



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.
(No tags found.)

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: Compare C# Java
Previous Resource: Some Useful and Efficient .Net commands
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use