【问题标题】:CryptographicException - Unable to update the passwordCryptographicException - 无法更新密码
【发布时间】:2013-11-25 06:49:34
【问题描述】:

我有一些想要保护的数据,所以我使用ProtectedData 将其加密到一个文件中。 当我尝试读取和解密数据时,我遇到了最奇怪的异常:

CryptographicException - 无法更新密码。为新密码提供的值不符合域的长度、复杂性或历史要求。

这是它被抛出的地方:

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

使用DataProtectionScope.CurrentUser时也会发生这种情况。

我没有在网上找到有关此异常的任何信息,所以我几乎一无所知。

【问题讨论】:

    标签: c# dpapi


    【解决方案1】:

    一些通用错误不会产生异常,并且会抛出最后一个错误。

    从 System.Security.Cryptography.ProtectedDate.Unprotect 内部:

    throw new CryptographicException(Marshal.GetLastWin32Error());
    

    更具体地说,它最像失败,因为使用 System.Security.Cryptography 实现 crypt32.dll: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;
        }
    

    【讨论】:

      猜你喜欢
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2021-01-11
      • 1970-01-01
      • 2022-01-27
      • 2021-11-11
      • 2017-03-03
      相关资源
      最近更新 更多