Search code examples
c#.netusing

'System.Security.Cryptography.Rfc2898DeriveBytes': type used in a using statement must be implicitly convertible to System.IDisposable


I have the code working in .Net framework 4.5 but when i try to code the same in .Net framework 2.0, it gives compilation error

'System.Security.Cryptography.Rfc2898DeriveBytes': type used in a using statement must be implicitly convertible to System.IDisposable

How to fix it?

CODE

[ComVisible(true)]
    public string HashPassword(string password)
    {

        byte[] salt;
        byte[] subkey;
        using (var deriveBytes = new Rfc2898DeriveBytes(password, SaltSize, PBKDF2IterCount))
        {
            salt = deriveBytes.Salt;
            subkey = deriveBytes.GetBytes(PBKDF2SubkeyLength);
        }

        var outputBytes = new byte[1 + SaltSize + PBKDF2SubkeyLength];
        //some more code goes here
        return Convert.ToBase64String(outputBytes);
    }

Solution

  • How about if you remove the using like:

     Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, SaltSize, 
     PBKDF2IterCount);
     salt = deriveBytes.Salt;
     subkey = deriveBytes.GetBytes(PBKDF2SubkeyLength);