我验证时,加密密码ans salt不匹配

时间:2018-01-04 20:48:37

标签: c# encryption

我使用以下代码来加密和保护密码,并向其添加盐,但当我尝试在用户登录时验证它时,我不知道为什么。

public static class Encrypt
{
    public static string saltValue { get; set; }
    public static string hashValue { get; set; }

    public static void SecurePassword(string password)
    {
        // Create a truly random salt using RNGCryptoServiceProvider.
        RNGCryptoServiceProvider csprng = new RNGCryptoServiceProvider();
        byte[] salt = new byte[32];
        csprng.GetBytes(salt);

        // Get the salt value
        saltValue = Convert.ToBase64String(salt);
        // Salt the password
        byte[] saltedPassword = Encoding.UTF8.GetBytes(saltValue + password);

        // Hash the salted password using SHA256
        SHA512Managed hashstring = new SHA512Managed();
        byte[] hash = hashstring.ComputeHash(saltedPassword);

        // Save both the salt and the hash in the user's database record.
        saltValue = Convert.ToBase64String(salt);
        hashValue = Convert.ToBase64String(hash);            
    }

    public static void ValidateLogin(string password, string username)
    {
        // Read the user's salt value from the database
        string saltValueFromDB = saltValue;

        // Read the user's hash value from the database
        string hashValueFromDB = hashValue;

        byte[] saltedPassword = Encoding.UTF8.GetBytes(saltValueFromDB + password);

        // Hash the salted password using SHA256
        SHA512Managed hashstring = new SHA512Managed();
        byte[] hash = hashstring.ComputeHash(saltedPassword);

        string hashToCompare = Convert.ToBase64String(hash);

        if (hashValueFromDB.Equals(hashToCompare))
            Console.WriteLine("User Validated.");
        else
            Console.WriteLine("Login credentials incorrect. User not validated.");
    }
}

请指教。提前谢谢

1 个答案:

答案 0 :(得分:1)

稍微更改了您的代码,但这有效:

    public class Encrypt
    {
        public HashedCredential SecurePassword(string password, string salt = "")
        {
            var saltValue = salt;
            if (string.IsNullOrEmpty(salt))
            {
                saltValue = GenertateSalt();
            }

            // Salt the password
            byte[] saltedPassword = Encoding.UTF8.GetBytes(saltValue + password);

            // Hash the salted password using SHA256
            SHA512Managed hashstring = new SHA512Managed();
            byte[] hash = hashstring.ComputeHash(saltedPassword);

            return new HashedCredential(saltValue, Convert.ToBase64String(hash));
        }

        private string GenertateSalt()
        {
            RNGCryptoServiceProvider csprng = new RNGCryptoServiceProvider();
            byte[] salt = new byte[32];
            csprng.GetBytes(salt);
            return Convert.ToBase64String(salt);
        }
    }

    public class HashedCredential
    {
        public string SaltValue { get; }
        public string HashValue { get; }

        public HashedCredential(string saltValue, string hashValue)
        {
            SaltValue = saltValue;
            HashValue = hashValue;
        }
    }

    [TestMethod]
    public void GenerateSalt()
    {
        // Arrange
        var sut = new Encrypt();

        // Act
        var result = sut.SecurePassword("Test");
        var resultB = sut.SecurePassword("Test", result.SaltValue);

        // Assert
        Console.WriteLine($"resultA:'{result.HashValue}'");
        Console.WriteLine($"resultB:'{resultB.HashValue}'");

        Assert.AreEqual(result.HashValue, resultB.HashValue);
    }