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