在.NET4中无法加密/解密SQLite数据库 [英] Cannot encrypt / decrypt SQLite database in .NET4

查看:1000
本文介绍了在.NET4中无法加密/解密SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7 x64上运行Visual Studio 2010。我正在写的应用程序应该在所有平台(AnyCPU)上运行。如果我使用System.Data.SQLite.dll x86版本(sqlite-netFx40-static-binary-bundle-Win32-2010-1.0.77.0.zip)或x64,我可以加密/解密数据库文件一个(sqlite-netFx40-static-binary-bundle-x64-2010-1.0.77.0.zip)。



我需要我的应用程序在两个平台上运行x86和x64 (任何CPU项目构建设置)。我试过安装ADO.NET 4.0提供程序(SQLite-1.0.67.1-vs2010-net4-setup.exe)。我添加了参考(右键单击项目,添加参考,.NET选项卡 - > System.Data.SQLite)并运行该程序。如果我解密该文件并尝试通过调用ChangePassword(myPass)加密,我得到以下异常:


System.EntryPointNotFoundException is捕获


Message =无法在DLL'System.Data.SQLite.DLL'中找到一个名为'sqlite3_rekey'的入口点。
Source = System.Data.SQLite TypeName =
StackTrace:


 在System.Data.SQLite.SQLite3.ChangePassword(Byte [newPasswordBytes] 
在System.Data .SQLite.SQLiteConnection.ChangePassword(Byte [newPassword]
在System.Data.SQLite.SQLiteConnection.ChangePassword(String newPassword)
在SQLiteTest.Database.Encrypt()在C:\SQLiteTest\Database .cs:line 166




此外,我尝试使用SQLiteConnection对象打开一个连接,并在两种不同的情况下得到两个不同的异常。
首先,如果文件被加密,并且我没有在连接字符串中指定密码,我得到这个:


系统.Data.SQLite.SQLiteException被捕获
消息=文件打开
不是数据库文件文件被加密或不是数据库

Source = System.Data.SQLite ErrorCode = -2147467259
StackTrace:
在System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn,String strSql,SQLiteStatement previous,



UInt32 timeoutMS, String& strRemain)
在System.Data.SQLite.SQLiteCommand.BuildNextCommand()
在System.Data.SQLite.SQLiteCommand.GetStatement(Int32索引)
在System.Data.SQLite.SQLiteDataReader System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd,CommandBehavior behave)的
$ System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior
behavior)


$ b在System.Data.SQLite.SQL iteCommand.ExecuteNonQuery()
在System.Data.SQLite.SQLiteConnection.Open()
在SQLiteTest.Database.GetAllLanguages()在C:\SQLiteTest\Database.cs:第216行


其次,如果我添加了密码参数到连接字符串,我得到像上面那样的System.EntryPointNotFoundException。



所以,有人知道在AnyCPU平台上的C#应用​​程序中使用加密SQLite数据库的确切的方式吗?



感谢提前!

解决方案

以下解决方案有点脏,但可能会工作,我们没有使用加密数据库,如果这将为您工作不是100%。
我们下载了x64和x86的两个版本的sqlite dll,并将它们放在不同的文件夹中。我们手动加载它们,取决于当前正在运行的平台应用程序(我们检查IntPtr.Size,从.net 4在Environment类中有一个属性称为Is64BitOperatingSystem)当我们手动加载程序集时,我们获取连接实例与

  var sqliteConnectionType = assembly.GetType(System.Data.SQLite.SQLiteConnection); 
(DbConnection)Activator.CreateInstance(sqliteConnectionType);

我们不会在我们的项目中使用任何版本的sqlite。


I am running Visual Studio 2010 on a Windows 7 x64. The app I'm writing is supposed to run on all platforms (AnyCPU). I'm able to encrypt/decrypt the database file if I'm using the System.Data.SQLite.dll either x86 version (sqlite-netFx40-static-binary-bundle-Win32-2010-1.0.77.0.zip) or x64 one (sqlite-netFx40-static-binary-bundle-x64-2010-1.0.77.0.zip).

I need my app to run on both platforms x86 and x64 (Any CPU project build setting). I've tried installing the ADO.NET 4.0 Provider (SQLite-1.0.67.1-vs2010-net4-setup.exe). I added the reference (right-click on project, Add Reference, .NET tab -> System.Data.SQLite) and ran the program. If I decrypt the file and try to encrypt by calling ChangePassword("myPass"), I get the following exception:

System.EntryPointNotFoundException was caught

Message=Unable to find an entry point named 'sqlite3_rekey' in DLL 'System.Data.SQLite.DLL'. Source=System.Data.SQLite TypeName="" StackTrace:

   at System.Data.SQLite.UnsafeNativeMethods.sqlite3_rekey(IntPtr db, Byte[ key, Int32 keylen)
   at System.Data.SQLite.SQLite3.ChangePassword(Byte[ newPasswordBytes)
   at System.Data.SQLite.SQLiteConnection.ChangePassword(Byte[ newPassword)
   at System.Data.SQLite.SQLiteConnection.ChangePassword(String newPassword)
   at SQLiteTest.Database.Encrypt() in C:\SQLiteTest\Database.cs:line 166

Also, I've tried to open a connection using the SQLiteConnection object and I get two different exceptions in two different cases. First, if the file is encrypted and I don't specify a password in the connection string I get this:

System.Data.SQLite.SQLiteException was caught Message=File opened that is not a database file file is encrypted or is not a database
Source=System.Data.SQLite ErrorCode=-2147467259 StackTrace: at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous,

UInt32 timeoutMS, String& strRemain) at System.Data.SQLite.SQLiteCommand.BuildNextCommand() at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index) at System.Data.SQLite.SQLiteDataReader.NextResult() at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery() at System.Data.SQLite.SQLiteConnection.Open() at SQLiteTest.Database.GetAllLanguages() in C:\SQLiteTest\Database.cs:line 216

Second, if I add the password parameter to the connection string, I get the System.EntryPointNotFoundException like the one above.

So, does anyone know a sure-fire way to work with encrypted SQLite database in a C# app on AnyCPU platform?

Thanks in advance!

解决方案

Following Solution is a little bit dirty, but it may work, we didn't use encrypted database so i'm not 100% if that will work for you. We downloaded both versions of sqlite dlls for x64 and x86 and placed them in different folders. We load them manually dependent on what platform application is currently running ( we check IntPtr.Size , from .net 4 there is a property in Environment class called Is64BitOperatingSystem) when we loaded assembly manually we get connection instance with

 var sqliteConnectionType = assembly.GetType("System.Data.SQLite.SQLiteConnection");
 (DbConnection)Activator.CreateInstance(sqliteConnectionType);

we don't use reference to any version of sqlite in our project.

这篇关于在.NET4中无法加密/解密SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆