Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Data.Sqlite can support sqlcipher key options ? #21491

Closed
IOL0ol1 opened this issue Jul 2, 2020 · 2 comments
Closed

Microsoft.Data.Sqlite can support sqlcipher key options ? #21491

IOL0ol1 opened this issue Jul 2, 2020 · 2 comments
Labels
area-adonet-sqlite closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@IOL0ol1
Copy link

IOL0ol1 commented Jul 2, 2020

i can use Microsoft.Data.Sqlite.Core 5.0.0-preview.6.20312.4
and SQLitePCLRaw.bundle_e_sqlcipher 2.0.3
to create a sqlcipher database file with custom sqlcipher key options.
but i can't open it.

I think it's because the sql query is used in SqliteConnection.Open code here

sqlcipher key options need after sqlite3_open_v2 but before other sql query.

my code like this:

       public void ChangeKeyOptionTest()
        {
            try
            {
                string db = "sqlcipher4.db";
                string pwd = "password";
                // delete db file
                if (File.Exists(db)) File.Delete(db);
                // create sqlcipher database
                CreateOrOpenSqlcipher(db, pwd);
                // open sqlcipher database
                CreateOrOpenSqlcipher(db, pwd);
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"{ex}");
            }

        }

        public void CreateOrOpenSqlcipher(string db, string pwd)
        {
            using (var conection = new SqliteConnection($"DataSource='{db}';Password='{pwd}'"))
            {
                conection.Open();
                using (var cmd = conection.CreateCommand())
                {
                    // change sqlcipher key options
                    cmd.CommandText = "PRAGMA cipher_memory_security = OFF;";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "PRAGMA kdf_iter = '10000';";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "PRAGMA cipher_page_size = 8192;";
                    cmd.ExecuteNonQuery();
                    //create table make db file is not zero size
                    cmd.CommandText = "CREATE TABLE [t_data]([c_id] INTEGER PRIMARY KEY);";
                    cmd.ExecuteNonQuery();
                }
            }
        }

maybe we can add action in SqliteConnectionStringBuilder, like

Action<SqliteConnection> BeforeOpen
Action<SqliteConnection> AfterOpen

and invoke in SqliteConnection.Open.

@ajcvickers
Copy link
Member

Putting this on the backlog to consider making it easier to send custom pragmas in this case. For now the way to do this is to send the pragmas directly, rather than using the connection string configuration.

@ajcvickers ajcvickers added this to the Backlog milestone Jul 6, 2020
@IOL0ol1
Copy link
Author

IOL0ol1 commented Jul 8, 2020

it's works for me.

        public void CreateOrOpenSqlcipher(string db, string pwd)
        {
              // do not add Password in connectionstring if need custom sqlcipher key options.
            using (var conection = new SqliteConnection($"DataSource='{db}';"))
            {
                conection.Open(); 
                using (var cmd = conection.CreateCommand())
                {
                    // set password by send the pragmas directly
                    cmd.CommandText = $"PRAGMA key = '{pwd}';";
                    cmd.ExecuteNonQuery();
                    // change sqlcipher key options
                    cmd.CommandText = "PRAGMA cipher_memory_security = OFF;";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "PRAGMA kdf_iter = '10000';";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "PRAGMA cipher_page_size = 8192;";
                    cmd.ExecuteNonQuery();
                    //create table make db file is not zero size
                    cmd.CommandText = "CREATE TABLE [t_data]([c_id] INTEGER PRIMARY KEY);";
                    cmd.ExecuteNonQuery();
                }
            }
        }

@IOL0ol1 IOL0ol1 closed this as completed Jul 8, 2020
@bricelam bricelam added closed-not-needed closed-no-further-action The issue is closed and no further action is planned. and removed closed-not-needed labels Aug 20, 2020
@bricelam bricelam removed this from the Backlog milestone Aug 20, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-adonet-sqlite closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants