Open Encrypted Sqlite Database With Microsoft.Data.Sqlite Created By System.Data.Sqlite

0

I have a database that was created with a password using the System.Data.Sqlite package and the ChangePassword method.

Is it possible to open this database using the Microsoft.Data.Sqlite package? I've tried using the sqlcipher package described here: https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/encryption?tabs=netcore-cli but I get the error:

Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 26: 'file is not a database'.
   at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
   at Microsoft.Data.Sqlite.SqliteCommand.<PrepareAndEnumerateStatements>d__64.MoveNext()
   at Microsoft.Data.Sqlite.SqliteCommand.<GetStatements>d__54.MoveNext()
   at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader()
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
   at Microsoft.Data.Sqlite.SqliteConnectionExtensions.ExecuteNonQuery(SqliteConnection connection, String commandText, SqliteParameter[] parameters)
   at Microsoft.Data.Sqlite.SqliteConnection.Open()

My connection string looks like:

Data Source=<full path to db>;Password=<password>

More information: Microsoft.Data.Sqlite.Core package 5.0.2 SQLitePCLRaw.bundle_e_sqlcipher package 2.0.4

I've also tried not specifying the password in the connection string but in a pragma statement after opening the connection string as described here: https://www.bricelam.net/2016/06/13/sqlite-encryption.html

but I get the same error.

 SqliteConnection liteConnection = new SqliteConnection(this.ConnectionString);  
            liteConnection.Open();
            using (var command = liteConnection.CreateCommand())
            {
                command.CommandText = "SELECT quote($password);";
                command.Parameters.AddWithValue("$password", _password);
                var quotedPassword = (string)command.ExecuteScalar();
                Console.WriteLine(quotedPassword);
                command.CommandText = "PRAGMA key = " + quotedPassword;
                command.Parameters.Clear();
                command.ExecuteNonQuery();
            }
sqlite
sqlite-net
microsoft.data.sqlite
asked on Stack Overflow Jan 27, 2021 by MadSkeletor • edited Jan 27, 2021 by MadSkeletor

1 Answer

0

Try to use cipher_migrate from SQLCipher. Maybe you have a different cipher version

                    connection.Open();
                    using var command = connection.CreateCommand();
                    command.CommandText = $"PRAGMA key = {DatabaseKey};PRAGMA cipher_migrate;";
                    command.Parameters.Clear();
                    command.ExecuteNonQuery();
answered on Stack Overflow Feb 2, 2021 by Pavel Kurianov

User contributions licensed under CC BY-SA 3.0