5

At the moment, my sqlite db file is NOT encrypted and it's copied from the assets folder to the application data/data/mypackage/databases folder.

Now I want to add the SQLCipher libraries to my project and start using them. Can I encrypt the db file and copy it to assets and use the same key inside the application? Is it possible to encrypt the database on Windows? What I need to do?

Luis Neves
  • 1,067
  • 2
  • 10
  • 21
  • The goal of SQLCipher is to help the *user* protect *their* data from others. You appear to be attempting to prevent the user from accessing their data. This will not work. "Can I encrypt the db file and copy it to assets and use the same key inside the application?" -- yes, but you are wasting everyone's time, because anyone who wants to can simply get the key out of your app and decrypt the database. If you do not want the user to access this data, do not put the data on the user's device. – CommonsWare Dec 16 '12 at 22:49
  • humm I see your point... So it's not possible, right? So using the scenario that I integrate SQLCipher on my application, I should let the user choose the password, is that it? So I'm not able to use a db from assets, I will need to build the db with statements? – Luis Neves Dec 16 '12 at 22:54
  • "So it's not possible, right?" -- I am not sure what "it" is. "is that it?" -- yes. "So I'm not able to use a db from assets, I will need to build the db with statements?" -- you could ship an unencrypted database in assets, *then* encrypt it with the user's chosen password using SQLCipher. – CommonsWare Dec 16 '12 at 23:09

1 Answers1

-2

i think we should do like that

SQLiteDatabase normalDB = null;
SQLiteDatabase cryptedDB = null;
normalDB = SQLiteDatabase.openDatabase(dbPath, "", null,
    SQLiteDatabase.OPEN_READONLY
    | SQLiteDatabase.NO_LOCALIZED_COLLATORS);

cryptedDB = SQLiteDatabase.openOrCreateDatabase(
    encrypteddbPath, Constants.CRYPT_KEY, null);

Cursor cursor;

cursor = normalDB.query(TABLE_CITY,
new String[] { FIELD_CITY_CODE, FIELD_NAME1,
    FIELD_NAME2, FIELD_NAME3 }, null, null, null,
    null, null);
int i = cursor.getCount();
while (i > 0) {
    ContentValues newLabelValues = new ContentValues();

newLabelValues.put(FIELD_CITY_CODE, cursor.getString(0));
        newLabelValues.put(FIELD_NAME1, cursor.getString(1));
        newLabelValues.put(FIELD_NAME2, cursor.getString(2));
        newLabelValues.put(FIELD_NAME3, cursor.getString(3));
        cryptedDB.insert(TABLE_CITY, null, newLabelValues);

cursor.moveToNext();
}
sharkbait
  • 2,980
  • 16
  • 51
  • 89