编译器错误消息:CS0103:当前上下文中不存在名称“ProtectedData”

时间:2013-08-27 01:16:51

标签: c# cryptography

尝试使用System.Security的加密代码时出现运行时错误。我添加了对System.Security的引用,一切看起来不错,但我收到此错误:“编译器错误消息:CS0103:当前上下文中不存在名称'ProtectedData'”

这是抛出错误的代码。

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);
    }

谢谢, 萨姆

1 个答案:

答案 0 :(得分:10)

您需要为System.Security.Cryptography添加using语句,并且需要引用System.Security.dll。从您的问题看来,您刚刚添加了引用而不是using语句。

您可以完全限定引用,而不是使用using语句:

byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
            Encoding.Unicode.GetBytes(ToInsecureString(input)), salt,
            System.Security.Cryptography.DataProtectionScope.CurrentUser);