There is rarely ever a reason to store an encrypted version of a password. That creates a security vulnerability. Instead, it is usually best to store a one-way hash (such as using SHA1) of the password combined with a random salt. Then you always compare the hash of entered passwords against hashes stored in the database, rather than ever actually comparing passwords.
The benefit of this approach is that no one can determine what a user's password is, even if he or she gains access to the database. And the salt makes identical passwords appear different from one another.
The following is an example of the creation of a random salt using the System.Security.Cryptography
namespace.
byte[] salt = new byte[10];
RandomNumberGenerator.Create().GetBytes(salt);
You can combine the salt with the password and generate a one-way hash as follows:
byte[] passwordBytes = new byte[Encoding.UTF8.GetByteCount(password) + salt.Length]; // Create buffer for password bytes and hash
int passwordLength = Encoding.UTF8.GetBytes(password, 0, password.Length, passwordBytes, 0);
salt.CopyTo(passwordBytes, passwordLength);
byte[] hash = null;
using (SHA512Managed hasher = new SHA512Managed()) {
hash = hasher.ComputeHash(passwordBytes);
}
Store both the hashed password and the salt. When authenticating a user, use the same salt as that used when creating the stored hash to hash the password entered by the user. Compare this new hash to the one in the database.