Question

I tried to use SQLCipher to encrypt/decrypt my database. It seems to work, but I want to be certain that I'm doing it correctly. Here is my code:

 public class MainActivity extends Activity { 

    private DataBaseCategory dbc;
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);   
            //this.deleteDatabase("category");
            dbc = new DataBaseCate gory(this); 
            try {
                   dbc.openToWriting(); 
                   dbc.createCategory("TEST4");  
                   dbc.close(); 
            } 
            catch(Throwable ex) { 
              Log.e("ABC","abc",ex);
            }
            dbc.openToReading(); 
            List<Category> list =  dbc.getAllCategories(); 
            System.out.println("Size : "+list.size()); 
            dbc.close();    
}

 public class DataBaseCategory {

    private SQLiteDatabase database; 
    private Context context;
    private DataBaseCategoryHelper dbCHelper;  
    private String password = "password";
    private String[] allColumns = { DataBaseCategoryHelper.COLUMN_ID,
        DataBaseCategoryHelper.COLUMN_CATEGORY_NAME };
    public DataBaseCategory(Context context) { 
            this.context = context; 
            SQLiteDatabase.loadLibs(context);  
            dbCHelper = new DataBaseCategoryHelper(this.context);
    }   
    public void openToWriting() throws SQLException{   
             database = dbCHelper.getWritableDatabase(password);  

    }    
    public void openToReading() throws SQLException { 
             database = dbCHelper.getReadableDatabase(password);
    } 
    public void close() {
             dbCHelper.close();
    }  
    /** 
      * Not important part of code 
      */  
 }
 public class DataBaseCategoryHelper extends SQLiteOpenHelper{ 

            public static final String TABLE_COMMENTS = "category";
            public static final String COLUMN_ID = "id";
            public static final String COLUMN_CATEGORY_NAME = "category_name"; 
            private static final String DATABASE_NAME = "category";
            private static final int DATABASE_VERSION = 1;
            private static final String DATABASE_CREATE = "create table "
                  + TABLE_COMMENTS + "(" + COLUMN_ID
                  + " integer primary key autoincrement, " + COLUMN_CATEGORY_NAME
                  + " text not null);";
      public DataBaseCategoryHelper(Context context) {
             super(context, DATABASE_NAME, null, DATABASE_VERSION);
      } 
      @Override
      public void onCreate(SQLiteDatabase database) {  
          try { 
              database.execSQL(DATABASE_CREATE);  

          } 
          catch(Exception ex ) { 
             Log.e("ErroeDBC ",ex.toString());
          }
      }

Is this really all I need to encrypt/decrypt the database ? Am I missing anything?

Was it helpful?

Solution

Other than collecting the password from the user, rather than hard-coding it, that is all you need. SQLCipher for Android is only incrementally different than using ordinary SQLite: just call loadLibs() before using it, then supply the password to getReadableDatabase(), getWriteableDatabase(), etc.

Over time, you may have more issues, such as when SQLCipher changes its database format and you need to perform an upgrade, as was needed from SQLCipher 2.x to 3.0.x. But those are infrequent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top