try {
getWritableDatabase(mPassword)
} catch (e: SQLiteException) {
encrypt(mPassword.toByteArray())
getWritableDatabase(mPassword)
}
private fun encrypt(passphrase: ByteArray) {
val originalFile = context.getDatabasePath(DB_NAME)
if (originalFile.exists()) {
val newFilePath = originalFile.parent
val newFile = File(newFilePath, "sqlcipherutils.db")
// get database version from existing database
val databaseVersion = SQLiteDatabase.openDatabase(
originalFile.absolutePath,
"",
null,
SQLiteDatabase.OPEN_READWRITE
).use { database ->
database.version
}
SQLiteDatabase.openOrCreateDatabase(
newFile.absolutePath, passphrase, null
).use {
it.rawExecSQL( **
> //line 164
**
"ATTACH DATABASE '${originalFile.absolutePath}' AS sqlcipher4 KEY '${
String(
mPassword
)
}'"
)
it.rawExecSQL("SELECT sqlcipher_export('main', 'sqlcipher4')")
it.rawExecSQL("DETACH DATABASE sqlcipher4")
it.version = databaseVersion
}
originalFile.delete()
newFile.renameTo(originalFile)
} else {
throw FileNotFoundException(originalFile.absolutePath.toString() + " not found")
}
}
Caused by: net.sqlcipher.database.SQLiteException: file is not a database
at net.sqlcipher.database.SQLiteDatabase.native_rawExecSQL(Native Method)
at net.sqlcipher.database.SQLiteDatabase.rawExecSQL(SQLiteDatabase.java:2455)
at .database.utils.DbHelper.encrypt(DbHelper.kt:164)