密码更改后,ProtectedData.Unprotect(DPAPI)停止工作

假设我加密数据并将其写入如下文件:

byte[] encrypted = ProtectedData.Protect(plain, null, DataProtectionScope.CurrentUser); File.WriteAllBytes(filename, encrypted); 

解密也很简单:

 byte[] encrypted = File.ReadAllBytes(filename); byte[] decrypted = ProtectedData.Unprotect(encrypted, null, DataProtectionScope.CurrentUser); 

现在,当我在Protect和Unprotect的调用之间更改Windows密码时,Unprotect将抛出exception。 我确实希望将加密数据链接到我的用户帐户,但我也希望它能够在密码更改后继续使用。

我想我必须将数据交给Windows,而不是将其写入我自己的文件,以便Windows可以在密码更改时重新加密它。 我只是找不到告诉我如何的文档。 有人知道吗?

你如何更改密码? 使用正常过程并指定旧密码应该与ProtectedData ,而在计算机管理下设置新密码则不行。

设置密码对话框并显示警告