使用ProtectedData.Unprotect函数的C#错误

问题描述

我正在尝试使用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,DataProtectionScope.LocalMachine );
                
            }catch (Exception e){
                
                Console.Write("Error: ");
                Console.WriteLine(e.Message);
                return null;
                
            }
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)