We have an application that encrypts/decrypts data as DataProtectionScope.LocalMachine
. We're now having to change the scope to DataProtectionScope.CurrentUser
.
Will the existing strings encrypted under the LocalMachine
scope still be readable when the scope is changed to CurrentUser
, assuming of course the user is logged into the same machine?
EDIT: I've written a very quick & dirty test application. Strangely, on the same computer I can decrypt a string encrypted under LocalMachine or CurrentUser scope by both LocalMachine & CurrentUser scopes. This doesn't sound like the correct behaviour, help!
private void btnUserEncrypt_Click(object sender, EventArgs e)
{
//encrypt data
var data = Encoding.Unicode.GetBytes(txtUserEncrypt.Text);
byte[] encrypted = ProtectedData.Protect(data, null, DataProtectionScope.CurrentUser);
txtUserEncrypt.Text = Convert.ToBase64String(encrypted);
}
private void btnUserDecrypt_Click(object sender, EventArgs e)
{
byte[] data = Convert.FromBase64String(txtUserDecrypt.Text);
//decrypt data
byte[] decrypted = ProtectedData.Unprotect(data, null, DataProtectionScope.CurrentUser);
txtUserDecrypt.Text = Encoding.Unicode.GetString(decrypted);
}
private void btnMachineEncrypt_Click(object sender, EventArgs e)
{
//encrypt data
var data = Encoding.Unicode.GetBytes(txtMachineEncrypt.Text);
byte[] encrypted = ProtectedData.Protect(data, null, DataProtectionScope.LocalMachine);
txtMachineEncrypt.Text = Convert.ToBase64String(encrypted);
}
private void btnMachineDecrypt_Click(object sender, EventArgs e)
{
byte[] data = Convert.FromBase64String(txtMachineDecrypt.Text);
//decrypt data
byte[] decrypted = ProtectedData.Unprotect(data, null, DataProtectionScope.LocalMachine);
txtMachineDecrypt.Text = Encoding.Unicode.GetString(decrypted);
}