Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
670 views
in Technique[技术] by (71.8m points)

c# - ProtectedData.Unprotect (DPAPI) stops working after password change

Suppose I encrypt data and write it to a file like this:

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

Decrypting is as straightforward:

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

Now when I change my windows password between the calls to Protect and Unprotect, Unprotect will throw an exception. I do want the encrypted data to be linked to my user account, but I also want it to survive password changes.

I guess I have to hand the data over to Windows rather than writing it to my own file, so that Windows can re-encrypt it on password changes. I just can't find the documentation that tells me how. Does anyone know?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How do you change the password? Using the normal procedure and specifying the old password should work with ProtectedData, while setting a new password under computer management wouldn't.

Set Password dialog with warning


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...