What everyone is saying is correct, it is good practice to Hash passwords rather than encrypt them.
Here is how you can use it for yours.
private const int SHAVALUE = 16; // Change this number to whatever you want. It's like your key
private const int CB = 20; // You can change this two if you want (For extra security, maybe)
private static string GetPasswordReadyForDatabaseStorage(string password)
{
var salt = new byte[SHAVALUE];
//Create the salt value with a cryptographic PRNG:
new RNGCryptoServiceProvider().GetBytes(salt);
//Create the Rfc2898DeriveBytes and get the hash value:
var pbkdf2 = new Rfc2898DeriveBytes(password, salt, 10000);
var hash = pbkdf2.GetBytes(CB);
//Combine the salt and password bytes for later use:
var hashBytes = new byte[SHAVALUE+CB];
Array.Copy(salt, 0, hashBytes, 0, SHAVALUE);
Array.Copy(hash, 0, hashBytes, SHAVALUE, CB);
//Turn the combined salt+hash into a string for storage
return Convert.ToBase64String(hashBytes);
}
private static bool VerifyPassword(string passwordUserEntered)
{
/* Fetch the stored value */
string getPasswordHash = savedPasswordHash;//<--- Get the hash password from the database and place it here.
/* Extract the bytes */
var hashBytes = Convert.FromBase64String(getPasswordHash);
/* Get the salt */
var salt = new byte[SHAVALUE];
Array.Copy(hashBytes, 0, salt, 0, SHAVALUE);
/* Compute the hash on the password the user entered */
var pbkdf2 = new Rfc2898DeriveBytes(passwordUserEntered, salt, 10000);
var hash = pbkdf2.GetBytes(CB);
/* Compare the results */
for (int i = 0; i < CB; i++)
if (hashBytes[i + SHAVALUE] != hash[i])
{
return false;
}
return true;
}
You can go even further and use SecureStrings instead of string parameters.
Hope this helps!
Here is the link to where i referenced this code
https://stackoverflow.com/a/10402129/251311