1

Trying to create functions to programmatically add a password to the sqlite database, and remove it - to get rid of the password when in debug.

I followed the steps of the best post explaining it (https://stackoverflow.com/a/1385690/6617804) and all the other sources that I found are using the same process.

What I am doing:

The connection strings:

    private static string AuthoringDbFullPath = System.IO.Path.Combine(DataFolder, "Authoring.db");
    private static string AuthoringConnectionStringWithPw = @"Data Source=" + AuthoringDbFullPath + "; Password=" + DbPassword +";";
    private static string AuthoringConnectionStringWithoutPw = @"Data Source=" + AuthoringDbFullPath+";";
    private static string AuthoringConnectionString = AuthoringConnectionStringWithPw;

The enable password function:

    public static bool EnableDbPassword()
    {
        try
        {
            //The Connection must not open closed to set a password
            Connection = new SQLiteConnection(AuthoringConnectionStringWithoutPw);
            Connection.SetPassword(DbPassword);

            Connection = new SQLiteConnection(AuthoringConnectionStringWithPw);
            Connection.Open();
            Connection.Close();

            if (Connection != null && Connection?.State == System.Data.ConnectionState.Open)
            {
                Connection.SetPassword(DbPassword);
                AuthoringConnectionString = AuthoringConnectionStringWithPw;
                return true;
            }
        }
        catch (Exception ex)
        {
            LogManager.LogException(ex);
        }
        return false;
    }

The disable password function:

    public static bool DisableDbPassword()
    {
        try
        {
            //if not connected, it does
            if (Connection == null || Connection?.State != System.Data.ConnectionState.Open)
            {
                Connection = new SQLiteConnection(AuthoringConnectionStringWithPw);
                Connection.Open();
            }
            Connection.Close();
            Connection.Open();

            if (Connection != null && Connection?.State == System.Data.ConnectionState.Open)
            {
                Connection.ChangePassword("");
                AuthoringConnectionString = AuthoringConnectionStringWithoutPw;
                return true;
            }
        }
        catch (Exception ex)
        {
            LogManager.LogException(ex);
        }
        return false;
    }

Two things happening:

  • After having add the password (with no apparent issue), I can still open the database in SQLiteStudio as if there were still no password:

    SQLiteStudio

  • When trying to disable after having enabled it, it raised a System.Data.SQLite.SqliteException exception when executing Connection.ChangePassword("");

    System.Data.SQLite.SqliteException :
    Message "file is not a database. not an error

How to create a simple architecture being able to add and remove a password on a SQLite database?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gaël
  • 117
  • 1
  • 2
  • 17

0 Answers0