SQLCipher - Decrypt database programmatically

1.5k views Asked by At

I'm using a .NET Core project in order to manage SQLite db encrypted and I would be able to create a db copy programmatically without encryption, but I'm not finding any sample code and I don't even know if this is possible.

I'm starting from scratch with the following project: https://github.com/paragpkulkarni/SQLiteEncryptionUsingEFCore

1

There are 1 answers

4
bit On BEST ANSWER

This is the final solution:

void DecryptDB(string sourceFilename, string destinationFilename, string password)
        {
            var connectionString = new SqliteConnectionStringBuilder
            {
                DataSource = sourceFilename,
                Mode = SqliteOpenMode.ReadWriteCreate,
                Password = password 
            };

            using var cn = new SqliteConnection(connectionString.ToString());
            cn.Open();

            using var c = cn.CreateCommand();
            c.CommandText = @$"ATTACH DATABASE '{destinationFilename}' AS plaintext KEY '';";
            var r = c.ExecuteNonQuery();

            c.CommandText = @"SELECT sqlcipher_export('plaintext')";
            r = c.ExecuteNonQuery();
        }