什么是创建和比较.NET中腌制的密码最简单的方法?最简单、密码、方法、NET

2023-09-02 20:46:58 作者:每一段路都是一种领悟

在不断努力提高自己的code我找的创建和.NET比较盐腌密码的最佳途径。

有没有更好的,更安全,办法这样做的?

我目前的code是如下:

 公共静态字符串CreateSaltedPassword(字符串盐,串密码)
    {
        SHA1CryptoServiceProvider SHA1 = NULL;

        SHA1 =新SHA1CryptoServiceProvider();

        //将字符串转换成字节数组
        字节[] =的byteValue System.Text.Encoding.UTF8.GetBytes(盐+密码);

        //计算哈希值
        byte []的byteHash = SHA1.ComputeHash(的byteValue);

        //处置非托管加密对象
        SHA1.Clear();

        返回Convert.ToBase64String(byteHash);
    }

    公共静态布尔ComparePasswords(字符串盐,字符串密码,串storedPassword)
    {
        字符串passwordHash =的String.Empty;

        //创建哈希密码
        passwordHash = PasswordProvider.CreateSaltedPassword(
            盐,密码);

        //比较密码
        回报(的String.Compare(storedPassword,passwordHash)== 0);
    }
 

解决方案 4分钟教你两种制作咸鸭蛋的腌制方法,非常简单,出油多还不太咸

您应该通过SHA1哈希做了几千次迭代:

 公共静态字符串CreateSaltedPassword(字符串盐,串密码,INT ITER)
{
  SHA1CryptoServiceProvider SHA1 =新SHA1CryptoServiceProvider();
  字节[] =的byteValue System.Text.Encoding.UTF8.GetBytes(盐+密码);
  做 {
    =的byteValue SHA1.ComputeHash(的byteValue);
  }而(--iter大于0);
  SHA1.Clear();
  返回Convert.ToBase64String(的byteValue);
}
 

做一些测试,看看需要多长时间你的目标机器上:1000〜10000次迭代不宜察觉的用户,当你正在检查他们的意见,但它会改变从小时攻击者的时间要求,以年—足够长的时间该密码将过期攻击者恢复它。

盐来源和大小没有示出;该盐不需要是秘密的,它只是需要取消predictable对于一个给定的密码。 8个字节的加密RNG是一种方便,安全的盐。用户名都比较方便,但可能会有点不太安全。

该设置给出了最合理的应用程序安全的今天。下一步在安全就会受到bcrypt。凡我所建议的散列函数以上的多次迭代,bcrypt做更多有条不紊一个非常缓慢的密钥生成过程。

您可能对这些类似的问题感兴趣,我的答案:

Encrypting密码 Salt

In an ongoing effort to improve my code I am looking for the best way to create and compare a salted password in .NET.

Is there a better, or more secure, way of doing this?

My current code is as follows:

    public static string CreateSaltedPassword(string salt, string password)
    {
        SHA1CryptoServiceProvider SHA1 = null;

        SHA1 = new SHA1CryptoServiceProvider();

        // Convert the string into an array of bytes
        byte[] byteValue = System.Text.Encoding.UTF8.GetBytes(salt + password);

        // Compute the hash value
        byte[] byteHash = SHA1.ComputeHash(byteValue);

        // Dispose the unmanaged cryptographic object 
        SHA1.Clear();

        return Convert.ToBase64String(byteHash);
    }

    public static bool ComparePasswords(string salt, string password, string storedPassword)
    {
        string passwordHash = string.Empty;

        // Create the hashed password
        passwordHash = PasswordProvider.CreateSaltedPassword(
            salt, password);

        // Compare the passwords
        return (string.Compare(storedPassword, passwordHash) == 0);
    }

解决方案

You should do a few thousand iterations through the SHA1 hash:

public static string CreateSaltedPassword(string salt, string password, int iter)
{
  SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
  byte[] byteValue = System.Text.Encoding.UTF8.GetBytes(salt + password);
  do {
    byteValue = SHA1.ComputeHash(byteValue);
  } while (--iter > 0);
  SHA1.Clear();
  return Convert.ToBase64String(byteValue);
}

Do some testing to see how long it takes on your target machine: 1000 to 10000 iterations shouldn't be noticeable to users when you are checking their input, but it will change an attacker's time requirement from hours to years—long enough that the password will have expired before the attacker recovers it.

The salt source and size is not shown; the salt does not need to be secret, it just needs to be unpredictable for a given password. 8 bytes from a cryptographic RNG is a convenient and secure salt. User names are more convenient, but may be a little less secure.

This set up gives reasonable security for most applications today. The next step up in security would be bcrypt. Where I suggested multiple iterations of the hash function above, bcrypt does more methodically with a very slow key generation process.

You may be interested in my answers to these similar questions:

Encrypting Passwords Salt