0

I have a windows application, the tartget framework is .NET Framework 4.6.1

I use c# to hash passwords. My code is

public string HashPassword(string password) { byte[] salt = new byte[128 / 8]; using (var rng = new RNGCryptoServiceProvider()) { rng.GetBytes(salt); } string hashed = ""; using (var rfc = new Rfc2898DeriveBytes(password, salt, 100000, HashAlgorithmName.SHA512)) { //logic implementation } } 

I got an error CS1729 'Rfc2898DeriveBytes' does not contain a constructor that takes 4 arguments.

I right click on Rfc2898DeriveBytes, then go to the Definition. In the namespace System.Security.Cryptography, there is '''public Rfc2898DeriveBytes(string password, byte[] salt, int iterations, HashAlgorithmName hashAlgorithm)''' ''' : this(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false).GetBytes(password), salt, iterations, hashAlgorithm)'''

So, why I got the error message? It seems the compiler is looking at somewhere else.

Thank you

4
  • According to docs, that constructor is applicable to .NET Framework 4.7.2 and upper. Commented Dec 7, 2023 at 10:50
  • 1
    Why don't you just upgrade your target framework version? Commented Dec 7, 2023 at 10:51
  • the solution uses many third parties projects, I worry it may cause these projects not working Commented Dec 7, 2023 at 22:17
  • If it is not available to 4.6.1, why I right click going to that file? Commented Dec 7, 2023 at 22:20

1 Answer 1

1

As Auditive said, Rfc2898DeriveBytes(string password, byte[] salt, int iterations, HashAlgorithmName hashAlgorithm) only supports .NET Framework 4.7.2 and above.

For details, please refer to: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rfc2898derivebytes.-ctor?view=net-8.0#system-security-cryptography-rfc2898derivebytes-ctor(system-string-system-byte()-system-int32-system-security-cryptography-hashalgorithmname)

In addition, as to why you can find this method after clicking "Go to definition" in .net framework 4.7.1, it is because you turned on "Enable navigation to decompiled sources", which caused you to see decompiled code.

Please follow these steps:

Tools=>Options=>Text Editor=>C#=>Advanced=>Uncheck "Enable navigation to decompiled sources"

enter image description here

Then you will find that there is no Rfc2898DeriveBytes(string password, byte[] salt, int iterations, HashAlgorithmName hashAlgorithm) after going to the definition.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.