Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I'm using DB Browser for SQLite to create the database and set the password. 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.

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:

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.

If it matters I'm using Visual Studio 2017 on Windows 10 64Bit.

Thanks.

like image 201
Ashley B Avatar asked Feb 22 '26 16:02

Ashley B


1 Answers

SQLiteConnection and DB Browser for SQLite use different kinds of encryption. You have to encrypt the DB using SQLiteConnection itself. Unfortunately you cannot use DB Browser afterwards

See encrypt with c#

I believe there is no solution instead of implementing the encryption by yourself into DB Browser. There is already a discussion on this topic, and the developers don't seem to plan an implementation: discussion here

like image 170
Rene Avatar answered Feb 24 '26 06:02

Rene