ProtectedData.Protect gives me the error "Windows Data Protection API (DPAPI) is not supported on this platform".
Using Visual Studio 2022, .Net 5.0, C# console application, and Windows 11.
My old programs are able to encrypt and decrypt passwords but new programs throw the same error. Is this due to my recent upgrade to Windows 11? Is there a feature in Windows I need to modify?
public string Encrypt(string password)
{
var encoding = new UTF8Encoding();
byte[] plain = encoding.GetBytes(password);
byte[] secret = ProtectedData.Protect(plain, null, DataProtectionScope.CurrentUser);
return Convert.ToBase64String(secret);
}
public string Decrypt(string password, EmailController EmailC)
{
byte[] secret = Convert.FromBase64String(password);
byte[] plain = ProtectedData.Unprotect(secret, null, DataProtectionScope.CurrentUser);
var encoding = new UTF8Encoding();
return encoding.GetString(plain);
}
M. Elghamry's comment got me going in the right direction.
I solved the problem by changing the target platform in my project's settings.