0

I'm trying to decrypt an encrypted SQLite DB (in .Net). According to Zetetic's documentation (https://www.zetetic.net/sqlcipher/sqlcipher-api/) one can use sqlcipher_export for this purpose, like so:

        var connection = new SqliteConnection(string.Format("Data Source={0}", fullDbPath));
        connection.Open();

        var key = //get it from somewhere
        var command = connection.CreateCommand();
        command.CommandText = "SELECT quote($password);";
        command.Parameters.AddWithValue("$password", key);
        var quotedPassword = (string)command.ExecuteScalar();

        command.Parameters.Clear();
        command.CommandText = $"PRAGMA key = {quotedPassword}; ATTACH DATABASE 'plaintext.db' AS plaintext KEY '';SELECT sqlcipher_export('plaintext');DETACH DATABASE plaintext";
        command.ExecuteNonQuery();

However, this throws an exception Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 1: 'table "__EFMigrationsHistory" already exists'.'

I can't figure out what I'm doing wrong, any ideas?

1 Answer 1

1

From my tests the code worked as expected and what it did, it EXPORTED the encrypted database to a new database file called plaintext.db.

Since you did not provide a full path for the plaintext.db file I'm not sure where it was generated, however if you were to provide a full filepath in the attach statement e.g: ATTACH DATABASE 'C:\Users\John\Data\plaintext.db' AS plaintext KEY ''; a file plaintext.db would be created in that folder and it would contain the unencrypted data from the original database.

As for the exception that you were receiving, it must have been because the export code was called multiple times, the database with its tables was already created and now the subsequent export calls were trying to create tables that already existed which failed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! You were right, I had previously tried to pass the full path to the plaintext db file to the command and got an error, I probably bungled something up, and assumed sqlcipher would just use the same folder as the input db.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.