如何在c#中读取加密的sqlite数据库

时间:2016-04-14 03:55:24

标签: c# sqlite encryption

我已经在这个问题上敲了一会儿 - 我不是一个出色的编码员,但我只能想象那里有一个我想念的简单解决方案。我一直在研究一些PoC代码,使用System.Data.SQLite数据提供程序创建加密的SQLite数据库。我正在用C#编写一个基本的控制台应用程序来帮助自己理解这是如何工作的。这是我的问题。

我可以在连接字符串中创建一个带有密码的新sqlite数据库(根据我的理解)加密数据库。这是完整的代码:

try
{
    if (!System.IO.File.Exists(@"c:\temp\test.db.sqlite"))
    {
        System.Data.SQLite.SQLiteConnection.CreateFile(@"c:\temp\test.db.sqlite");
    }

    System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(@"Data Source=c:\temp\test.db.sqlite;Version=3;password=abc");
    conn.Open();

    System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand(conn);
    //System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand("create table test (name char(50))", conn);
    //cmd.ExecuteNonQuery();


    cmd.CommandText = "insert into test values ('my string')";
    cmd.ExecuteNonQuery();

    cmd.CommandText = "select * from test";

    var dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        Console.WriteLine(dr.GetString(0));
    }

    conn.Close();
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
    throw;
}

这似乎工作得很好......我可以创建一个表,向表中添加数据然后查询表。下次我尝试运行它时,我将进行以下更改,以便不重新创建表格...否则,代码应该是相同的:

...

if (!System.IO.File.Exists(@"c:\temp\test.db.sqlite"))
{
    System.Data.SQLite.SQLiteConnection.CreateFile(@"c:\temp\test.db.sqlite");
}

System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(@"Data Source=c:\temp\test.db.sqlite;Version=3;password=abc");
conn.Open();

//System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand(conn);
System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand("create table test (name char(50))", conn);
cmd.ExecuteNonQuery();


cmd.CommandText = "insert into test values ('my string')";

...

当我第二次运行它时,我收到以下错误:

System.Data.SQLite.SQLiteException (0x80004005): file is encrypted or is not a database file is encrypted or is not a database
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(CommandBehavior behavior)

知道我做错了什么吗?如何重新打开并使用我创建并加密的数据库?

1 个答案:

答案 0 :(得分:0)

设置密码:

string conn = @"Data Source=database.s3db;Password=Mypass;";
SQLiteConnection connection= new SQLiteConnection(conn);
connection.Open();
//Some code
connection.ChangePassword("Mypass");
connection.Close();

更改密码:

string conn = @"Data Source=database.s3db;";
SQLiteConnection connection= new SQLiteConnection(conn);
connection.Open();
//Some code
connection.ChangePassword("Mypass");
connection.Close();

可以通过连接字符串连接密码:

string conn = @"Data Source=database.s3db;Password=Mypass;";

或:

string conn = @"Data Source=database.s3db;";
SQLiteConnection connection= new SQLiteConnection(conn);
connection.Open();
//Some code
connection.SetPassword("Mypass");
connection.Close();