admin管理员组

文章数量:1395024

using (var pbkdf2 = new Rfc2898DeriveBytes(
                m_Password,
                l_saltBytes,
                Convert.ToInt32(ConstantFatcorString),
                HashAlgorithmName.SHA256))
{l_bytes = pbkdf2.GetBytes(32);}

I am using .NET framework 4.8. Can anyone help with a supporting link? SaltString=test; ConstantFatcorString=1000;

using (var pbkdf2 = new Rfc2898DeriveBytes(
                m_Password,
                l_saltBytes,
                Convert.ToInt32(ConstantFatcorString),
                HashAlgorithmName.SHA256))
{l_bytes = pbkdf2.GetBytes(32);}

I am using .NET framework 4.8. Can anyone help with a supporting link? SaltString=test; ConstantFatcorString=1000;

Share Improve this question edited Mar 27 at 7:20 Peter Smith 5,5529 gold badges52 silver badges79 bronze badges asked Mar 27 at 7:05 Nishant SharmaNishant Sharma 11 bronze badge 11
  • Please do not delete and repost the same question. stackoverflow/questions/79537814/… – mjwills Commented Mar 27 at 7:55
  • Please share a minimal reproducible example. – mjwills Commented Mar 27 at 7:55
  • You already said in your other question that your salt was only four bytes. The error is salt is not at least eight bytes. I mean, I am not sure what to say to you other than "you really should read the error message". I mean it tells you what you have done wrong, quite clearly. Your salt needs to be, and I quote, at least 8 bytes. – mjwills Commented Mar 27 at 7:56
  • Iam converting salt byte[] l_saltBytes = Encoding.UTF8.GetBytes(m_sSaltString); – Nishant Sharma Commented Mar 27 at 8:26
  • I just want is there any API which dnt have limitation for salt for eigth bytes for PBKDF2 (SHA256) algortithm – Nishant Sharma Commented Mar 27 at 8:28
 |  Show 6 more comments

1 Answer 1

Reset to default 1

How are you converting your salt to a byte array? If using UTF8 (like the below):

            var l_saltBytes = Encoding.UTF8.GetBytes("test");

the result will be 4 bytes long - so this is expected - the salt needs to be bigger than 8 bytes. The salt is used with the password to create keys. If the salt is fewer than 8 bytes then it doesn't add enough randomness to the generated keys, so the result is very vulnerable to brute force attacks due to lack of entropy.

Page 5 of the RFC spec explains this in some detail:
https://www.rfc-editor./rfc/rfc2898.txt

Note the section:

      1. It is difficult for an opponent to precompute all the keys
         corresponding to a dictionary of passwords, or even the most
         likely keys. If the salt is 64 bits long, for instance, there
         will be as many as 2^64 keys for each password. An opponent is
         thus limited to searching for passwords after a password-based
         operation has been performed and the salt is known.

So even though this is an example, it talks about 64 bits long (there are 8 bits in a byte), and this has been taken as a sensible minimum within Rfc2898DeriveBytes.

A good salt needs to be at least this length, and should be random. Guids are quite often used (you can generate one using Guid.NewGuid().ToString() but will obviously need to save it to validate the password) - so to get your code working try using something like that - but it is by design that the salt must be at least 8 bytes in length.

The following works:

        static void Main(string[] args)
        {
            string ConstantFatcorString = "1000";

            var m_Password = "this is my passowrd";
            var l_saltBytes = Encoding.UTF8.GetBytes("testtest");
            byte[] l_bytes = null;

            using (var pbkdf2 = new Rfc2898DeriveBytes(
                       m_Password,
                       l_saltBytes,
                       1000,
                       HashAlgorithmName.SHA256))
            {
                l_bytes = pbkdf2.GetBytes(32);
            }
        }

Please ensure you use a more sensible value for salt though for the reasons given :)

本文标签: cRfc2898DeriveBytes giving error salt is not at least eight bytesStack Overflow