我在 C# 中试验ProtectData.Protect()
和函数。ProtectData.Unprotect()
我编写了一个程序,将加密数据写入 SQLite 数据库,然后读取数据库并解密数据。每次我运行我的代码时,我都会收到以下错误:
Unhandled Exception: System.Security.Cryptography.CryptographicException: The parameter is incorrect.
at System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope)
at MyProgram.ReadData(SQLiteConnection conn)
at MyProgram.Main()
完整的代码很长,但这里是decrypt()
函数。我正在传递一个字节数组;我已使用 验证该类型为字节数组myByteData.GetType()
,它返回System.Byte[]
.
public static byte [] decrypt( byte [] data ) {
byte [] s_additionalEntropy = null;
try {
//Decrypt
return ProtectedData.Unprotect( data, s_additionalEntropy, DataProtectionScope.CurrentUser );
}catch{
try {
return ProtectedData.Unprotect( data, s_additionalEntropy, DataProtectionScope.LocalMachine );
}catch (Exception e){
Console.Write("Error: ");
Console.WriteLine(e.Message);
return null;
}
}
}