C#密码加密

时间:2011-06-28 20:49:31

标签: c# asp.net encryption

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

        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行上生成错误。

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

5 个答案:

答案 0 :(得分:14)

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

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

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

您可以在此处详细了解:Salting Your Password: Best Practices?

答案 1 :(得分:2)

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

如果其他人在这里是正确的,请使用下面示例类中的salted哈希。以下摘自“Another example of how to store a salted password hash

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;
    }
}

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

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

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

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

我说错了什么,纠正我XD

答案 4 :(得分:0)

请改为尝试:

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