我们有一个应用程序将数据加密/解密为DataProtectionScope.LocalMachine
. 我们现在必须将范围更改为DataProtectionScope.CurrentUser
.
LocalMachine
当范围更改为时,在范围下加密的现有字符串是否仍然可读CurrentUser
,当然假设用户登录到同一台机器?
编辑:我写了一个非常快速和肮脏的测试应用程序。奇怪的是,在同一台计算机上,我可以解密在 LocalMachine 或 CurrentUser 范围下由 LocalMachine 和 CurrentUser 范围加密的字符串。这听起来不像是正确的行为,求助!
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);
}