I am trying to open an encrypted database created with Room and SQLCipher 4.14 on Flutter but so far have no success. For unencrypted database I got it to work, just not the encrypted one.
I tried both the SQLite3MultipleCiphers approach and the encrypted_drift approach on Encryption . Both of them gives me code 26 exception. I tried the unecrypted version and it works, so it must be something able the encryption. I have checked the hexKey and the byteArray and ensured it was correctly converted. Can anyone tell me what am I missing here?
net.zetetic.database.sqlcipher.SQLiteNotADatabaseException: file is not a database (code 26): , while compiling: SELECT COUNT(*) FROM sqlite_schema;
The Kotlin code:
val factory = SupportOpenHelperFactory(key) // Key is a ByteArray
val instance = Room.databaseBuilder(
appContext, RoomManager::class.java, "app_db.sqlite"
).openHelperFactory(factory).build()
SQLite3MultipleCiphers approach
final hexKey = hex.encode(key ?? []);
driftDatabase(
name: 'app_db',
native: DriftNativeOptions(
databaseDirectory: () async {
final dir = await getApplicationDocumentsDirectory();
return join(dir.parent.path, 'databases');
},
setup: (database) {
database.execute("PRAGMA cipher = 'sqlcipher';");
database.execute("PRAGMA legacy = 4;");
database.execute("PRAGMA key = \"x'$hexKey'\";");
final check = database.select("SELECT count(*) FROM sqlite_master;");
debugPrint("Decryption successful. Found ${check.length} tables.");
},
),
);
encrypted_drift approach
final hexKey = hex.encode(key ?? []);
EncryptedExecutor.inDatabaseFolder(
path: path,
password: hexKey,
);