0

I am just wondering what all the security experts think of my method for password hashing. I want to come up with a method I can use for all my future web development projects.

First I create a random salt based on a 32 byte array :

RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
byte[] randomNumber = new byte[32];
provider.GetBytes(randomNumber);
string salt = System.Text.Encoding.UTF8.GetString(randomNumber);

I would then store that salt in the user's database row.

To hash the password, I would use :

// Create a new instance of the RijndaelManaged
// class. This generates a new key and initialization
// vector (IV).
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
myRijndael.GenerateKey();
myRijndael.GenerateIV();
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes(salt + password, myRijndael.Key, myRijndael.IV);
string encryptedPassword = System.Text.Encoding.UTF8.GetString(encrypted);
}

The EncryptStringToBytes and DecryptStringFromBytes methods can be seen here : http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx

If anyone could look this over and let me know of any vulnerabilities or places where I could strengthen it, I would greatly appreciate it! Thanks!

Edit

I've implemented a version of BCrypt here :

        string myPassword = "password";

        string mySalt = BCrypt.GenerateSalt();

        string myHash = BCrypt.HashPassword(myPassword, mySalt);
Professed3376
  • 183
  • 1
  • 7
  • 1
  • You're using UTF-8 encoding on binary data. Thanks to unfortunate defaults in .net, that means they get corrupted silently. Use Base64 instead. 2) When you use UTF8Encoding, create your own instance, with throw-on-error enabled. That way you don't run into silent corruption. 3) I don't see any hash in your code 4) 32 bytes is overkill. Id use 16.
  • – CodesInChaos Oct 14 '13 at 09:31
  • Besides the mistakes others have pointed out: There is never a most secure way to hash something. You can always add a longer salt or a hash algorithm with a longer key length to increase the exponent of the time it takes to brute-force. At one point you have reached a method which is sufficiently secure, but there is no theoretical upper limit. – Philipp Oct 14 '13 at 15:17