I have tried to follow all the examples out there for encrypting a Android Room Database. The code executes, but when I check the state, it comes back as unencrypted. I do not see nay errors in the log.
The following are the libraries I am using.
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;
import net.sqlcipher.database.SQLiteDatabaseHook;
The following is my application code
static CheckingInRoomDatabase getDatabase(final Context context) {
Log.i(TAG, "CheckingInRoomDatabase getDatabase: In Checking In Room Database");
if (INSTANCE == null) {
Log.i(TAG, "CheckingInRoomDatabase getDatabase Instance == null first: ");
synchronized (CheckingInRoomDatabase.class) {
if (INSTANCE == null) {
Log.i(TAG, "CheckingInRoomDatabase getDatabase Instance == null second: ");
char[] ch = AppConfig.PASSPHRASE;
// Declaring a byte array
byte[] passPhrase = new byte[ch.length];
// Iterating over the char array
for (int i = 0; i < ch.length; i++) {
// Converting each char into its byte equivalent
passPhrase[i] = (byte) ch[i];
}
String testStatus = AppConfig.DEBUG;
if (testStatus.equals("Prod")) {
final SupportFactory factory = new SupportFactory(passPhrase);
Log.i(TAG, "CheckingInRoomDatabase getDatabase Instance == null testStatus= " + testStatus);
INSTANCE =
Room.databaseBuilder(context,
CheckingInRoomDatabase.class,
"checking_in_database.db")
.fallbackToDestructiveMigration()
.openHelperFactory(factory)
.addCallback(sRoomDatabaseCallback)
.build();
Log.i(TAG, "CheckingInRoomDatabase getDatabase: running populates from builder" + INSTANCE);
try {
// Get database state
SQLCipherUtils.State state = SQLCipherUtils.getDatabaseState(context, "checking_in_database.db");
// Check whether database is encrypted
if (state == SQLCipherUtils.State.UNENCRYPTED) {
Log.i(TAG, "CheckingInRoomDatabase getDatabase: database is not encrypted");
Bugfender.e("CheckingInRoomDatabase getDatabase", "database is not encrypted");
} else if (state == SQLCipherUtils.State.ENCRYPTED) {
// Otherwise, good to go
Log.i(TAG, "CheckingInRoomDatabase getDatabase: database is encrypted");
Bugfender.i("CheckingInRoomDatabase getDatabase ", "database is encrypted. No action necessary.");
} else if (state == SQLCipherUtils.State.DOES_NOT_EXIST) {
// No database found. Normal if first launch
Log.i(TAG, "CheckingInRoomDatabase getDatabase: database not found");
Bugfender.i("CheckingInRoomDatabase getDatabase ", "database not found");
}
} catch (Exception e) {
Bugfender.e("CheckingInRoomDatabase ", "Failed to get database encryption state!" + e);
}
}
else {
INSTANCE =
Room.databaseBuilder(context,
CheckingInRoomDatabase.class,
"checking_in_database.db")
.fallbackToDestructiveMigration()
.addCallback(sRoomDatabaseCallback)
.build();
}
}
}
Log.i(TAG, "CheckingInRoomDatabase getDatabase: database being populated");
}
Log.i(TAG, "CheckingInRoomDatabase getDatabase database returned: ");
return INSTANCE;
}
The following is my SupportFactory.class
package com.grgmobilesolutions.peepsconnection;
import androidx.sqlite.db.SupportSQLiteOpenHelper;
import net.sqlcipher.database.SQLiteDatabaseHook;
public class SupportFactory implements SupportSQLiteOpenHelper.Factory {
private final byte passphrase;
private final SQLiteDatabaseHook hook;
private final boolean clearPassphrase;
public SupportFactory(byte[] passphrase) {
this(passphrase, (SQLiteDatabaseHook)null);
}
public SupportFactory(byte[] passphrase, SQLiteDatabaseHook hook) {
this(passphrase, hook, true);
}
public SupportFactory(byte[] passphrase, SQLiteDatabaseHook hook,
boolean clearPassphrase) {
this.passphrase = passphrase;
this.hook = hook;
this.clearPassphrase = clearPassphrase;
}
@Override
public SupportSQLiteOpenHelper create(SupportSQLiteOpenHelper.Configuration configuration) {
return new SupportHelper(configuration, passphrase, hook, clearPassphrase);
}
}
The following is my SupportHelper.class
//package com.grgmobilesolutions.peepsconnection;
package com.grgmobilesolutions.peepsconnection;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;
import net.sqlcipher.database.SQLiteDatabaseHook;
public class SupportHelper implements SupportSQLiteOpenHelper {
private SQLiteOpenHelper openHelper;
public SupportHelper(final Configuration configuration, byte[] password, SQLiteDatabaseHook hook,
boolean enableWriteAheadLogging) {
this(configuration, password, hook, enableWriteAheadLogging, configuration.callback.version);
}
public SupportHelper(final Configuration configuration, byte[] password, SQLiteDatabaseHook hook,
boolean enableWriteAheadLogging, int minimumSupportedVersion) {
}
@Nullable
@Override
public String getDatabaseName() {
return null;
}
@NonNull
@Override
public SupportSQLiteDatabase getReadableDatabase() {
return null;
}
@NonNull
@Override
public SupportSQLiteDatabase getWritableDatabase() {
return null;
}
@Override
public void close() {
}
@Override
public void setWriteAheadLoggingEnabled(boolean b) {
}
}