5

I have worked with DBFlow which is quite simple to play around database but i want to ask is there a good example to use SQLCipher with DBFlow

I have Folowed this link for help

Raizlabs/DBFlow

But if any one can provide some simple example will be very helpful to create secure database application with simple steps.

farrukh
  • 627
  • 4
  • 16

2 Answers2

6

The usage doc linked by ConductedClever doesn't look like much but this is indeed almost all you need. Except for some details which could be mentioned:

I had to add @aar to the dbflow-cipher dependency in build.gradle to get it past gradle in the first place:

// build.gradle
def dbflow_version = "3.1.1"
def sqlcipher_version = "3.5.4"

dependencies {
  annotationProcessor "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version}"
  compile "com.github.Raizlabs.DBFlow:dbflow-core:${dbflow_version}"
  compile "com.github.Raizlabs.DBFlow:dbflow:${dbflow_version}"
  compile "com.github.Raizlabs.DBFlow:dbflow-sqlcipher:${dbflow_version}@aar"
  compile "net.zetetic:android-database-sqlcipher:${sqlcipher_version}@aar"
}

Change the cipher secret as preferred:

// SQLCipherHelperImpl.java
public class SQLCipherHelperImpl extends SQLCipherOpenHelper {

  public SQLCipherHelperImpl(DatabaseDefinition databaseDefinition, DatabaseHelperListener listener) {
    super(databaseDefinition, listener);
  }

  @Override
  protected String getCipherSecret() {
    return "your-cipher-secret";
  }
}

If you followed the dbflow getting started guide and your database is called AppDatabase then this is the class you should pass to new DatabaseConfig.Builder(AppDatabase.class) when initializing DBFlow:

// AppDatabase.java
@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION)
public class AppDatabase {

  public static final String NAME = "AppDatabase";

  public static final int VERSION = 1;
}

// DatabaseApplication.java
public class DatabaseApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    FlowManager.init(new FlowConfig.Builder(this)
      .addDatabaseConfig(
        new DatabaseConfig.Builder(AppDatabase.class)
          .openHelper(new DatabaseConfig.OpenHelperCreator() {
            @Override
            public OpenHelper createHelper(DatabaseDefinition databaseDefinition, DatabaseHelperListener helperListener) {
              return new SQLCipherHelperImpl(databaseDefinition, helperListener);
            }
          })
          .build())
      .build());
  }
}

Export your database and try to open it in an SQLite Client. This should fail now cause of the encryption.

Thomas
  • 319
  • 3
  • 7
  • Thanks, it is working if I install it as a fresh app but when I install this app on top of the old app which is not encrypted it is failing. Error: `net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;` Can you please suggest how to fix it? – Shailendra Madda Apr 22 '21 at 14:20
  • @ShailendraMadda I had to remove the old database file to fix this. Right before the call to `FlowManager.init(...)` I get the database file path with `final File dbPath = getDatabasePath(AppDatabase.NAME + ".db");` and delete it with `boolean deleteSuccess = dbPath.delete();` if it exists. Same for the temp database file `final File dbPathTemp = getDatabasePath("temp-" + AppDatabase.NAME + ".db");` This is encapsulated in an app version check to only delete the old database if the installed app version is older than the one which introduced scl cipher. – Thomas Jul 15 '21 at 14:33
  • Thanks for your reply will check it – Shailendra Madda Jul 15 '21 at 14:37
0

I haven't try it yet but this is the usage doc that the Official developer provides about using SQLcipher:

SQL Cipher & DBFlow

ConductedClever
  • 4,175
  • 2
  • 35
  • 69