这是用MD5加密密码的好方法吗?

Mar*_*oDS 0 c# encryption passwords md5

我以前从来没有加密过密码,这是我在这篇文章的帮助下想到的.这篇文章没有包括盐,所以我不得不自己弄清楚:

        UTF8Encoding encoder = new UTF8Encoding();
        byte[] salt = new byte[8];
        new Random().NextBytes(salt);
        byte[] encodedPassword = encoder.GetBytes(txtPassword.Text);
        byte[] saltedPassword = new byte[8 + encodedPassword.Length];
        System.Buffer.BlockCopy(salt, 0, saltedPassword, 0, 8);
        System.Buffer.BlockCopy(encodedPassword, 0, saltedPassword, 8, encodedPassword.Length);
        byte[] encryptedPassword = new MD5CryptoServiceProvider().ComputeHash(saltedPassword);
        byte[] saltedEncryptedPassword = new byte[8 + encryptedPassword.Length];
        System.Buffer.BlockCopy(salt, 0, saltedEncryptedPassword, 0, 8);
        System.Buffer.BlockCopy(encryptedPassword, 0, saltedEncryptedPassword, 8, encryptedPassword.Length);
Run Code Online (Sandbox Code Playgroud)

saltedEncryptedPassword存储在数据库中.正如您可能注意到的那样,在将涉及盐的字节数组连接在一起时遇到了一些麻烦.我做得对吗,还是有更好的方法?谢谢.

SLa*_*aks 6

密码哈希应该很,而不是快.
哈希越快,攻击者可以通过密码字典运行得越快.

因此,您不应该使用SHA之类的通用哈希来表示密码.

相反,使用经过验证的慢速技术,如scrypt或PBKDF2.