C# 将密码存储更改为在C窗体中存储哈希

C# 将密码存储更改为在C窗体中存储哈希,c#,sql,encryption,hash,sql-server-2014,C#,Sql,Encryption,Hash,Sql Server 2014,我当前的项目使用Microsoft的成员身份管理用户的凭据 目前,它将密码以明文形式存储到SQL DB中 我想改为存储哈希,或者加密/隐藏密码列 当前代码: 1个可能的解决方案:HashBytes 2可能的解决方案:C存储哈希 3种可能的解决方案:Transact-SQL加密单个列 我可以知道什么是正确的方法吗?谢谢。不用说,任何解决方案都比当前的好 第一种溶液看起来是固体的,但我无法确定是否使用了盐。如果不是,那就是一个劣势。您现在还依赖于此数据库供应商,无法切换 第二个解决方案看起来不错,但

我当前的项目使用Microsoft的成员身份管理用户的凭据

目前,它将密码以明文形式存储到SQL DB中

我想改为存储哈希,或者加密/隐藏密码列

当前代码:

1个可能的解决方案:HashBytes

2可能的解决方案:C存储哈希

3种可能的解决方案:Transact-SQL加密单个列


我可以知道什么是正确的方法吗?谢谢。

不用说,任何解决方案都比当前的好

第一种溶液看起来是固体的,但我无法确定是否使用了盐。如果不是,那就是一个劣势。您现在还依赖于此数据库供应商,无法切换

第二个解决方案看起来不错,但我没有仔细看,你可能应该去,他们会对工作代码进行检查,以寻求改进

最后一个解决方案并不是真正的解决方案。密码被散列而不是加密是有原因的。如果您可以解密它们,那么访问系统并窃取它们的攻击者将拥有与您相同的解密方式访问权限。所以这是一层不便,而不是安全

所以,选择第二个,让别人检查它的弱点或bug

protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();

        if (Page.IsValid)
        {
            NAME_OF_TABLE_Provider provider = new NAME_OF_TABLE_Provider();

            string userName = SPContext.Current.Web.CurrentUser.Name;
            MembershipUserCollection userMembership = Membership.GetAllUsers();

            MembershipUser membershipUser = userMembership[userName];

            if (membershipUser != null)
            {
                try
                {
                    membershipUser.ChangePassword(OldPassword.Text, NewPassword.Text);
                    ConfirmPanel.Visible = true;
                    InvalidPanel.Visible = false;
                    Message.InnerText = "The password is changed successfully";
                }
                catch (Exception ex)
                {
                    ConfirmPanel.Visible = false;
                    InvalidPanel.Visible = true;
                    InvalidPassword.InnerText = "The password is not strong. Please verify.";
                }
            }
        }
    }
INSERT INTO <tbl> (..., passwd) values (...., HashBytes('SHA1', @password))

SELECT HashBytes('SHA1', @password);
    static void Main(string[] args)
    {
        //Store a password hash:
        PasswordHash hash = new PasswordHash("password");
        byte[] hashBytes = hash.ToArray();
        //For testing purposes
        Console.WriteLine(Convert.ToBase64String(hashBytes));

        //Check password against a stored hash
        byte[] hashBytes2 = hashBytes;//read from store.
        PasswordHash hash2 = new PasswordHash(hashBytes2);

        if (!hash.Verify("password"))
        {
            throw new System.UnauthorizedAccessException();
        }
        else
        {
            Console.WriteLine("True");
        }
        Console.ReadLine();
    }

    }

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