2015-09-29 47 views
0

我有以下代碼和應用程​​序有時成功,但對於某些用戶它無法解密密碼。它主要發生在服務器和多用戶環境,在開發機器上效果很好。受保護的數據API問題

public static byte [] Protect(byte [] data) 
    { 
     try 
     { 
      // Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted 
      // only by the same current user. 
      return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.CurrentUser); 
     } 
     catch (CryptographicException e) 
     { 
      Console.WriteLine("Data was not encrypted. An error occurred."); 
      Console.WriteLine(e.ToString()); 
      return null; 
     } 
    } 

    public static byte [] Unprotect(byte [] data) 
    { 
     try 
     { 
      //Decrypt the data using DataProtectionScope.CurrentUser. 
      return ProtectedData.Unprotect(data, s_aditionalEntropy, DataProtectionScope.CurrentUser); 
     } 
     catch (CryptographicException e) 
     { 
      Console.WriteLine("Data was not decrypted. An error occurred."); 
      Console.WriteLine(e.ToString()); 
      return null; 
     } 
    } 
+1

按代碼DataProtectionScope.CurrentUser其用戶特定的,以便確保在多用戶環境中,其保護方法被稱爲實例的每個用戶登錄。我的建議是驗證問題變更範圍DataProtectionScope.LocalMachine和驗證,然後您可以確認 –

+0

感謝您的快速回復讓我檢查 –

+0

@sumeet kumar:LocalMachine範圍工作正常..感謝 –

回答

0

DataProtectionScope.LocalMachine:此範圍是有效的解密任何身份驗證的用戶在系統中。

DataProtectionScope.CurrentUser:此範圍僅對身份用於加密的用戶有效,只有身份可以使其解密。

public static byte [] Protect(byte [] data) 
     { 
      try 
      { 
       return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine); 
      } 
      catch (CryptographicException e) 
      { 
       Console.WriteLine("Data was not encrypted. An error occurred."); 
       Console.WriteLine(e.ToString()); 
       return null; 
      } 
     } 

     public static byte [] Unprotect(byte [] data) 
     { 
      try 
      { 
       return ProtectedData.Unprotect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine); 
      } 
      catch (CryptographicException e) 
      { 
       Console.WriteLine("Data was not decrypted. An error occurred."); 
       Console.WriteLine(e.ToString()); 
       return null; 
      } 
     } 
0

在服務器端環境中,您有一些問題需要使用它。查看詳情:

CurrentUser範圍:受保護的數據與相關CurrentUser,我的意思是,只有加密的數據可以實現解密它的用戶 - 沒有其他人。您可以將其理解爲保護個人數據的例程。

LocalMachine Scope:如上所述,它允許不同用戶解密數據,但可能導致安全問題!使用此範圍,即使不在同一組/域中的用戶也會解密數據!該控件不在加密例程中,而是在用戶訪問thar服務器中。

如果你有一個公共(或不屬於域)服務器,並需要一些球員來訪問某些類型的數據,你可能放棄DataProtectionScope並嘗試定製程序,其中:

1 - 你檢查用戶是否獲得授權。 2 - 您提供加密和解密數據的機制。 3 - 您可以對不同的用戶或組採用不同的密鑰。

細節,請考慮才能看到此鏈接: https://msdn.microsoft.com/en-us/library/system.security.cryptography.dataprotectionscope(v=vs.110).aspx

+0

代碼由其他人完成,我維護它,所以我不想實現任何其他的關鍵機制。 LocalMachine範圍適用於我 –