简体   繁体   English

无法使用C#打开受密码保护的SQLite 3数据库

[英]Can't open a password protected SQLite 3 database with C#

I'm having an issue using a password protected SQLite database, using System.Data.SQLite. 我使用System.Data.SQLite使用受密码保护的SQLite数据库时遇到问题。

I'm using DB Browser for SQLite to create the database and set the password. 我正在使用DB Browser for SQLite来创建数据库并设置密码。 With DB Browser I have no issues opening, entering the password viewing data, then closing the database. 使用数据库浏览器,我没有打开任何问题,输入密码查看数据,然后关闭数据库。

So with .NET 4.6.2 and System.Data.SqLite 1.0.105.2 the following code snippet does not work I keep getting a "file is encrypted or is not a database" error. 因此,对于.NET 4.6.2和System.Data.SqLite 1.0.105.2,以下代码片段不起作用我不断收到“文件已加密或不是数据库”错误。

namespace licensekeygeneration
{
    using NLog;
    using NLog.Extensions.AzureTableStorage;
    using System;
    using System.Data.SQLite;
    using System.Linq;
    using System.Windows;

/// <summary>Interaction logic for App.xaml</summary>
public partial class App : Application
{
    /// <summary>Make sure that NLog is running</summary>
    private static Logger logger = LogManager.GetCurrentClassLogger();

    /// <summary>Run before the application starts up</summary>
    void App_Startup(object sender, StartupEventArgs e)
    {
        try
        {
            // Set link to the SQLite database and grab the logging endpoint
            string dataSource = @"Data Source=c:\users\fred\desktop\database.db;Version=3;Page Size=1024;Password=ABCD";
            SQLiteConnection conn = new SQLiteConnection(dataSource);

            DataContext LocalDB = new DataContext(conn);

            // Sets the target for NLog in code 
            string strNlog = LocalDB.GetTable<TblS3Settings>().Where(item => item.StrSettingName.Equals("NlogEndPoint") && item.BoolIsValid.Equals(true)).ToList().FirstOrDefault().StrSettingValue;
            var azureStorageTarget = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName("AzureTableStorage");
            azureStorageTarget.ConnectionString = strNlog;
        }
        catch (Exception ex)
        {
            // Problem with the database or the connection so error out
            MessageBox.Show("There is an issue with the internal database\n" + ex.Message, "Application", MessageBoxButton.OK, MessageBoxImage.Hand);
            Current.Shutdown();
        }

        // Logging OK and we have an attached database so lets start
        MainWindow.Show();
    }
}

If I remove the password from the database using DB Browser for SQLite and I change the following line: 如果我使用DB Browser for SQLite从数据库中删除密码,我将更改以下行:

string dataSource = @"Data Source=c:\users\fred\desktop\database.db;Version=3;Page Size=1024;";
SQLiteConnection conn = new SQLiteConnection(dataSource);

I get the information I expect and life is good, So am I missing something with System.Data.SQLite as I just can't get it to work as I expected. 我得到了我期望的信息,生活很美好,所以我错过了System.Data.SQLite的东西,因为我无法让它像我预期的那样工作。

If it matters I'm using Visual Studio 2017 on Windows 10 64Bit. 如果重要的是我在Windows 10 64Bit上使用Visual Studio 2017。

Thanks. 谢谢。

SQLiteConnection and DB Browser for SQLite use different kinds of encryption. SQLite的SQLiteConnection和DB Browser使用不同种类的加密。 You have to encrypt the DB using SQLiteConnection itself. 您必须使用SQLiteConnection本身加密数据库。 Unfortunately you cannot use DB Browser afterwards 不幸的是,之后您无法使用数据库浏览器

See encrypt with c# 请参阅使用c#加密

I believe there is no solution instead of implementing the encryption by yourself into DB Browser. 我相信没有解决方案,而不是自己在DB Browser中实现加密。 There is already a discussion on this topic, and the developers don't seem to plan an implementation: discussion here 已经有关于这个主题的讨论,开发人员似乎没有计划实现: 这里讨论

The accepted way to set the password seams to be as follows: 设置密码接缝的可接受方式如下:

string dataSource = @"Data Source=c:\users\Fred\desktop\test.db;Version=3;Page Size=1024;";
            SQLiteConnection conn = new SQLiteConnection(dataSource);
            conn.Open();
            conn.ChangePassword("ABCD");
            conn.Close(); 

This "should" set the password to the database to ABCD thus: 这个“应该”将数据库的密码设置为ABCD:

string dataSource = @"Data Source=c:\users\Fred\desktop\test.db;Version=3;Page Size=1024;Password=ABCD;";
            SQLiteConnection conn = new SQLiteConnection(dataSource);
            conn.Open();

And this works which would appear to be correct but the following also works: 这项工作似乎是正确的,但以下也有效:

string dataSource = @"Data Source=c:\users\Fred\desktop\test.db;Version=3;Page Size=1024;";
            SQLiteConnection conn = new SQLiteConnection(dataSource);
            conn.Open();

Please note that after setting the password I can open a password protected database in any utility without supplying the password. 请注意,设置密码后,我可以在任何实用程序中打开受密码保护的数据库,而无需提供密码。

So unless I'm missing the point it would appear that password setting in the System.Data.SQLite is suspect. 因此,除非我忽略了这一点,否则看起来System.Data.SQLite中的密码设置是可疑的。

Try this: 尝试这个:

 var command = connection.CreateCommand();
        command.CommandText = "SELECT quote($password);";
        command.Parameters.AddWithValue("$password", _password);
        var quotedPassword = (string)command.ExecuteScalar();
        command.CommandText = "PRAGMA key = " + quotedPassword;
        command.Parameters.Clear();
        command.ExecuteNonQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM