2013-11-25 52 views
2

我有一些數據希望保護,所以我使用ProtectedData將它加密到文件上。 當我試圖讀取和解密數據時,我收到了最奇怪的異常:CryptographicException - 無法更新密碼

CryptographicException - 無法更新密碼。爲新密碼提供的值不符合域的長度,複雜性或歷史要求。

這是它被拋出:

byte[] decryptedData = ProtectedData.Unprotect(Encoding.UTF8.GetBytes(fileContent), 
Encoding.UTF8.GetBytes(entropy), 
DataProtectionScope.LocalMachine); 

使用DataProtectionScope.CurrentUser時,也發生了。

我還沒有找到關於這個異常的任何信息在線,所以我非常無能。

回答

0

某些通用錯誤不會生成異常並引發最後一個錯誤。

從內System.Security.Cryptography.ProtectedDate.Unprotect:

throw new CryptographicException(Marshal.GetLastWin32Error()); 

更具體地說,它是最喜歡的,因爲使用實施crypt32.dll的System.Security.Cryptography默認標誌的失敗:CryptUnprotectData - CRYPTPROTECT_UI_FORBIDDEN - 「此標誌用於不提供用戶界面(UI)的遠程情況,當此標誌設置並且爲保護或取消保護指定UI時,調用將失敗,並且GetLastError()返回ERROR_PASSWORD_RESTRICTION狀態碼「。 Windows Data Protection

我發現,工作對我來說是不使用中的Base64轉換器,我使用PowerShell使用相同的腳本解決方法:

static byte[] ByteArrayFromString(string s) 
    { 
     int length = s.Length/2; 
     byte[] numArray = new byte[length]; 
     if (s.Length > 0) 
     { 
      for (int i = 0; i < length; i++) 
      { 
       numArray[i] = byte.Parse(s.Substring(2 * i, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture); 
      } 
     } 
     return numArray; 
    }