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
<code class="language-csharp">byte[] salt = new byte[16]; new RNGCryptoServiceProvider().GetBytes(salt);</code>
Step 2: Hash Creation
<code class="language-csharp">var pbkdf2 = new Rfc2898DeriveBytes(password, salt, 100000); byte[] hash = pbkdf2.GetBytes(20);</code>
Step 3: Concatenating Salt and Hash
<code class="language-csharp">byte[] hashBytes = new byte[36]; Array.Copy(salt, 0, hashBytes, 0, 16); Array.Copy(hash, 0, hashBytes, 16, 20);</code>
Step 4: Base64 Encoding for Storage
<code class="language-csharp">string savedPasswordHash = Convert.ToBase64String(hashBytes);</code>
Step 5: Password Verification
<code class="language-csharp">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...</code>
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!