Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler Error Message: CS0103: The name 'ProtectedData' does not exist in the current context

I am getting a runtime error when trying to use System.Security's crypto code. I added a reference to System.Security and everything looks good but I am getting this error: "Compiler Error Message: CS0103: The name 'ProtectedData' does not exist in the current context"

here is the code that is throwing the error.

public static string EncryptString(SecureString input, string entropy)
    {
        byte[] salt = Encoding.Unicode.GetBytes(entropy);
        byte[] encryptedData = ProtectedData.Protect(
            Encoding.Unicode.GetBytes(ToInsecureString(input)),
            salt,
            DataProtectionScope.CurrentUser);
        return Convert.ToBase64String(encryptedData);
    }

Thanks, Sam

like image 593
user2719830 Avatar asked Dec 16 '22 07:12

user2719830


1 Answers

You need to add a using statement for System.Security.Cryptography and you need a reference to System.Security.dll. From your question it appears you just added the reference and not the using statement.

Instead of the using statement you can also fully qualify the references like this:

byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
            Encoding.Unicode.GetBytes(ToInsecureString(input)), salt,
            System.Security.Cryptography.DataProtectionScope.CurrentUser);
like image 87
pstrjds Avatar answered Dec 18 '22 10:12

pstrjds