I’m a beginner. I read this topic(Encrypting database with Room createFromAsset() method - #2 by commonsguy) and tried to imitate it, but it doesn’t work.
The log says database is encrypted. But when I look at the contents of the database by Database Inspecter,it’s empty.Thank in advance for any help.
String str = "pass";
char[] pass = str.toCharArray();
Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "dictionary_word16")
.createFromAsset("database/dictionary_word14")
.addMigrations(MIGRATION_1_2)
.addMigrations(MIGRATION_3_4)
.addMigrations(MIGRATION_4_5)
.addMigrations(MIGRATION_5_6)
.allowMainThreadQueries()
.build();
SQLCipherUtils.State state = SQLCipherUtils.getDatabaseState(getApplicationContext(), "dictionary_word16");
Log.d("TAG", String.valueOf(state));
if (state == SQLCipherUtils.State.UNENCRYPTED) {
try {
SQLCipherUtils.encrypt(getApplicationContext(), "dictionary_word16", pass);
Log.d("TAG", "Successfully encrypted database!");
} catch (IOException e) {
Log.e("TAG", "Failed to encrypt previously unencrypted database!");
e.printStackTrace();
}
}
final byte[] passphrase = SQLiteDatabase.getBytes(pass);
final SupportFactory factory = new SupportFactory(passphrase);
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "dictionary_word16")
.allowMainThreadQueries()
.openHelperFactory(factory)
.build();