2013-08-27 37 views
2

嘗試使用System.Security的加密代碼時出現運行時錯誤。我添加了一個對System.Security的引用,並且一切看起來不錯,但是出現此錯誤:「編譯器錯誤消息:CS0103:名稱'ProtectedData'在當前上下文中不存在'編譯器錯誤消息: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); 
    } 

感謝, 山姆

回答

7

您需要添加一個using語句System.Security.Cryptography,你需要System.Security.dll參考。從你的問題看來,你只是添加了引用而不是使用語句。

using語句,而不是你也可以完全限定引用這樣的:

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