C#密码加密

我发现一些在线代码可以很好地用于我想做的事情。 我需要一些能够加密密码,将其保存到数据库并轻松检索的东西。 以下代码几乎可以满足我的一切需求。

string UserName = txtUser.Text; string password = txtPass.Text; string encrKey = "keyvalue"; byte[] byteKey = { }; byte[] IV = {25, 47, 60, 88, 99, 106, 125, 139}; byteKey = Encoding.UTF8.GetBytes(encrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputArray = Encoding.UTF8.GetBytes(password); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byteKey, IV), CryptoStreamMode.Write); cs.Write(inputArray, 0, inputArray.Length); cs.FlushFinalBlock(); password = Convert.ToBase64String(ms.ToArray()); SqlCommand cmd = new SqlCommand("INSERT INTO USers (UserName, Password) VALUES (@UserName, @Password)", myConnection); cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@UserName", UserName); cmd.Parameters.AddWithValue("@Password", password); SqlDataReader rdr = cmd.ExecuteReader(); 

我遇到的问题是当密码为8个字符或更长时间时出现代码错误。 我收到此错误:

System.Security.Cryptography.CryptographicException:指定的键不是此算法的有效大小。 在Cryptostream行上生成错误。

我需要为我的钥匙使用不同的类型吗?

通常的做法是不加密数据库中的密码,而是对其进行哈希处理。
当用户尝试登录时,您将获取其键入的密码,将其哈希并与存储在数据库中的哈希进行比较。

行业标准哈希算法是SHA-1,可在.NET中读取。

为了更高的安全性,您在散列中使用“Salt”。

您可以在此处阅读更多相关信息: Salting Your Password:Best Practices?

如果您需要实际反转加密,只需使用ProtectedData类: http : //msdn.microsoft.com/en-us/library/system.security.cryptography.protecteddata.aspx

如果其他人在这里是正确的,请使用盐水哈希,如下面的示例类中所示。 以下摘自“ 另一个如何存储盐渍密码哈希的例子 ”

 public sealed class PasswordHash { const int SaltSize = 16, HashSize = 20, HashIter = 10000; readonly byte[] _salt, _hash; public PasswordHash(string password) { new RNGCryptoServiceProvider().GetBytes(_salt = new byte[SaltSize]); _hash = new Rfc2898DeriveBytes(password, _salt, HashIter).GetBytes(HashSize); } public PasswordHash(byte[] hashBytes) { Array.Copy(hashBytes, 0, _salt = new byte[SaltSize], 0, SaltSize); Array.Copy(hashBytes, SaltSize, _hash = new byte[HashSize], 0, HashSize); } public PasswordHash(byte[] salt, byte[] hash) { Array.Copy(salt, 0, _salt = new byte[SaltSize], 0, SaltSize); Array.Copy(hash, 0, _hash = new byte[HashSize], 0, HashSize); } public byte[] ToArray() { byte[] hashBytes = new byte[SaltSize + HashSize]; Array.Copy(_salt, 0, hashBytes, 0, SaltSize); Array.Copy(_hash, 0, hashBytes, SaltSize, HashSize); return hashBytes; } public byte[] Salt { get { return (byte[])_salt.Clone(); } } public byte[] Hash { get { return (byte[])_hash.Clone(); } } public bool Verify(string password) { byte[] test = new Rfc2898DeriveBytes(password, _salt, HashIter).GetBytes(HashSize); for (int i = 0; i < HashSize; i++) if (test[i] != _hash[i]) return false; return true; } } 

散列密码比加密好得多。 您将密码的哈希值存储在数据库中,并且您不再关心正常的文本密码。 当用户登录时,您获取正常的文本密码,对其进行哈希并比较两个哈希值(即数据库中的哈希值和您从用户输入中哈希的哈希值)进行身份validation。 这里的明显好处是你确保没有人 – 无论他访问数据库的原因 – 都会知道原始密码(理论上)。

我建议你用bcrypt。 http://code.google.com/p/bcryptnet/上提供的源代码下载并使用它。 但在使用之前。 阅读文档并了解它是如何工作的以及为什么bcrypt ……这很重要。

通过我的研究几个星期关于密码加密。 我终于找到了最适合我需要的bcrypt。 (我认为它最适合密码,如果我错了,请纠正我)

它是单向加密。 就像少数几个程序员所说的那样,哈希并比较而不是解密。

希望这能帮助你。 如果你发现有趣的事情请告诉我XD和平~~

我说错了什么,纠正我XD

试试这个:

 var hash = Encoding.ASCII.GetBytes(password); var sha1 = new SHA1CryptoServiceProvider(); var sha1hash = sha1.ComputeHash(hash); var hashedPassword = Encoding.ASCII.GetString(sha1hash);