Storing passwords using simple encryption is insufficient. Hashing, a one-way cryptographic function, provides a much stronger solution by transforming passwords into unique, irreversible, fixed-length values.
In 2012, PBKDF2 (Password-Based Key Derivation Function 2) was the recommended algorithm for .NET applications. The Rfc2898DeriveBytes
class facilitated a secure hashing process through these key steps:
Step 1: Salt Generation
byte[] salt = new byte[16]; new RNGCryptoServiceProvider().GetBytes(salt);
Step 2: Hash Creation
var pbkdf2 = new Rfc2898DeriveBytes(password, salt, 100000); byte[] hash = pbkdf2.GetBytes(20);
Step 3: Concatenating Salt and Hash
byte[] hashBytes = new byte[36]; Array.Copy(salt, 0, hashBytes, 0, 16); Array.Copy(hash, 0, hashBytes, 16, 20);
Step 4: Base64 Encoding for Storage
string savedPasswordHash = Convert.ToBase64String(hashBytes);
Step 5: Password Verification
byte[] hashBytes = Convert.FromBase64String(savedPasswordHash); byte[] salt = new byte[16]; Array.Copy(hashBytes, 0, salt, 0, 16); var pbkdf2 = new Rfc2898DeriveBytes(password, salt, 100000); byte[] hash = pbkdf2.GetBytes(20); // Compare hash values here...
The iteration count (100000 in these examples) within PBKDF2 controls the computational cost of cracking. While 10000 is the minimum suggested, adjust this value based on your application's performance needs. Higher iteration counts increase security but may impact application speed.
The above is the detailed content of How Does PBKDF2 Secure Password Storage in .NET (2012)?. For more information, please follow other related articles on the PHP Chinese website!