Hi, I’m having issues using SQLCipher on a newly created database w/ Room. I’m using a singleton to get the instance, and it works properly to write, delete, get data. But then, if I try to download the db from Android Device manager to open on DB Browser for SQLite for example, it asks for password but when I open it, it has no data at all, no tables at all and then it gets corrupted. When I try to open with Appspector, the database gets corrupted directly.
This is the code I’m using to retrieve the database, which occurs after the user types its password (which is on the server):
abstract class AppDatabase : RoomDatabase() {
...
companion object : SingletonHolder<AppDatabase, Application>({ app ->
Timber.v("Initializing DB")
val passphrase: ByteArray = SQLiteDatabase.getBytes(app.tmpKey!!.toCharArray())
val factory = SupportFactory(passphrase, null, false)
val db =
Room.databaseBuilder(
app,
AppDatabase::class.java,
"example.db"
)
.openHelperFactory(factory)
Timber.v("DB was successfully initialized")
db.build()
})
}
Opening w/ DB Browser causes:
Caused by: net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:91)
at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:48)
at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:60)
at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:2016)
at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1902)
at net.sqlcipher.database.SQLiteDatabase.keyDatabase(SQLiteDatabase.java:2673)
at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2603)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1247)
at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1322)
at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:166)
at net.sqlcipher.database.SupportHelper.getWritableDatabase(SupportHelper.java:83)
at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:476)
at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:281)
at androidx.room.RoomDatabase.query(RoomDatabase.java:324)
at androidx.room.util.DBUtil.query(DBUtil.java:83)
Opening w/ Appspector causes:
2020-04-09 16:56:49.269 29735-29845/xx E/SQLiteLog: (26) file is encrypted or is not a database
2020-04-09 16:56:49.278 29735-29845/xx E/DefaultDatabaseErrorHandler: Corruption reported by sqlite on database: /data/user/0/.../databases/test.db
2020-04-09 16:56:49.278 29735-29845/xx E/DefaultDatabaseErrorHandler: deleting the database file: /data/user/0/...databases/test.db