簡體   English   中英

如何在c#中讀取加密的sqlite數據庫

[英]How to read an encrypted sqlite db in c#

我一直在敲打這個問題一段時間 - 我不是一個出色的編碼員,但我只能想象有一個我想念的簡單解決方案。 我一直在研究一些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)

知道我做錯了什么嗎? 如何重新打開並使用我創建並加密的數據庫?

設置密碼:

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM