@@ -35,11 +35,19 @@ public override void ConnectionOpened(DbConnection connection, ConnectionEndEven
3535 // Pre-derived raw key — SQLCipher loads it directly, no PBKDF2 (~0 ms)
3636 cmd . CommandText = $ "PRAGMA key = \" { _encryptionKey } \" ;";
3737 cmd . ExecuteNonQuery ( ) ;
38+
39+ // Raw key still needs cipher params to match how the DB was encrypted
40+ cmd . CommandText = "PRAGMA cipher_page_size = 4096;" ;
41+ cmd . ExecuteNonQuery ( ) ;
42+ cmd . CommandText = "PRAGMA cipher_hmac_algorithm = HMAC_SHA512;" ;
43+ cmd . ExecuteNonQuery ( ) ;
44+ cmd . CommandText = "PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA512;" ;
45+ cmd . ExecuteNonQuery ( ) ;
3846 }
3947 else
4048 {
4149 // Passphrase fallback — SQLCipher runs PBKDF2(256000) internally (~20–50 ms)
42- cmd . CommandText = $ "PRAGMA key = '{ _encryptionKey } ';";
50+ cmd . CommandText = $ "PRAGMA key = '{ _encryptionKey } ';";
4351 cmd . ExecuteNonQuery ( ) ;
4452
4553 cmd . CommandText = "PRAGMA cipher_page_size = 4096;" ;
@@ -53,16 +61,6 @@ public override void ConnectionOpened(DbConnection connection, ConnectionEndEven
5361 }
5462 }
5563
56- // WAL mode is persistent in the database file — no-op if already set, upgrades fresh DBs.
57- // NORMAL synchronous is safe with WAL and reduces flush overhead on every commit.
58- using ( var cmd = connection . CreateCommand ( ) )
59- {
60- cmd . CommandText = "PRAGMA journal_mode = WAL;" ;
61- cmd . ExecuteNonQuery ( ) ;
62- cmd . CommandText = "PRAGMA synchronous = NORMAL;" ;
63- cmd . ExecuteNonQuery ( ) ;
64- }
65-
6664 base . ConnectionOpened ( connection , eventData ) ;
6765 }
6866
@@ -77,6 +75,14 @@ public override async Task ConnectionOpenedAsync(DbConnection connection, Connec
7775 // Pre-derived raw key — SQLCipher loads it directly, no PBKDF2 (~0 ms)
7876 cmd . CommandText = $ "PRAGMA key = \" { _encryptionKey } \" ;";
7977 await cmd . ExecuteNonQueryAsync ( cancellationToken ) ;
78+
79+ // Raw key still needs cipher params to match how the DB was encrypted
80+ cmd . CommandText = "PRAGMA cipher_page_size = 4096;" ;
81+ await cmd . ExecuteNonQueryAsync ( cancellationToken ) ;
82+ cmd . CommandText = "PRAGMA cipher_hmac_algorithm = HMAC_SHA512;" ;
83+ await cmd . ExecuteNonQueryAsync ( cancellationToken ) ;
84+ cmd . CommandText = "PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA512;" ;
85+ await cmd . ExecuteNonQueryAsync ( cancellationToken ) ;
8086 }
8187 else
8288 {
@@ -95,16 +101,6 @@ public override async Task ConnectionOpenedAsync(DbConnection connection, Connec
95101 }
96102 }
97103
98- // WAL mode is persistent in the database file — no-op if already set, upgrades fresh DBs.
99- // NORMAL synchronous is safe with WAL and reduces flush overhead on every commit.
100- using ( var cmd = connection . CreateCommand ( ) )
101- {
102- cmd . CommandText = "PRAGMA journal_mode = WAL;" ;
103- await cmd . ExecuteNonQueryAsync ( cancellationToken ) ;
104- cmd . CommandText = "PRAGMA synchronous = NORMAL;" ;
105- await cmd . ExecuteNonQueryAsync ( cancellationToken ) ;
106- }
107-
108104 await base . ConnectionOpenedAsync ( connection , eventData , cancellationToken ) ;
109105 }
110106}
0 commit comments