using SQLCipher with android shipped sqlite database file

前端 未结 3 1104
无人共我
无人共我 2020-12-09 23:21

I have a database file reset in the assets file.

How can I use SQLCipher to encrypt the database in android?

相关标签:
3条回答
  • 2020-12-09 23:47

    The following code excerpt can be used to create multiple tables in an SQLite Cipher database:

    Use these imports:

    import java.sql.SQLException;
    import net.sqlcipher.database.SQLiteDatabase;
    import net.sqlcipher.database.SQLiteOpenHelper;
    import android.content.ContentValues;  
    import android.content.Context;  
    import android.database.Cursor;  
    import android.util.Log;
    
    /** Helper to the database, manages versions and creation */
    
        public class DBAdapter extends SQLiteOpenHelper {
            private static final String DATABASE_NAME = "Test";
            private static final int DATABASE_VERSION = 1;
    
            // Table name
            public static final String TABLE_1 = "Table1";
            public static final String TABLE_2 = "Table2";
    
            // Column names for Table1 table
            static final String KEY_PASSWORD = "password";
            static final String KEY_USER = "user";
    
            // Column names for Table2 table
            static final String KEY_EVENT = "event";
            static final String KEY_USERNAME = "username";
    
    
            public DBAdapter(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }
    
            @Override
            public void onCreate(SQLiteDatabase db) {
                String sql1 = "create table " + Table_1 + " (" + KEY_USER + " text primary key, " + KEY_PASSWORD + " text not null);";
                String sql2 = "create table " + Table_2 + " (" + KEY_EVENT + " text primary key, " + KEY_USERNAME + " text not null FOREIGN KEY(" + KEY_USERNAME + ") REFERENCES " + TABLE_1 + "(" + KEY_USER + "));";
                db.execSQL(sql1);
                db.execSQL(sql2);
            }
    
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                if (oldVersion >= newVersion){
                    return;
                }
    
                String sql = null;
                if (oldVersion == 1) 
                    sql = "alter table " + TABLE_1 + " add note text;";
                if (oldVersion == 2)
                    sql = "";
    
                Log.d("EventsData", "onUpgrade  : " + sql);
                if (sql != null)
                    db.execSQL(sql);
            } 
    
            public Cursor getAllUsers(String username, SQLiteDatabase db){
                return db.query(TABLE_1, ...);
            }
    
            public Cursor getAllEvents(String event, SQLiteDatabase db){
                return db.query(TABLE_2, ...);
            }
       }
    

    Now you can do all the CRUD methods for both tables. Just make sure that each method has an SQLiteDatabase as an argument as shown in the getAllUsers() method.

    0 讨论(0)
  • 2020-12-09 23:53

    This is going to be a bit complicated. Since the database file format is different between SQLite and SQLCipher for Android, and since you want to ship an unencrypted database, you will have to do a few things.

    First, I'd get SQLiteAssetHelper going, to deliver the unencrypted database to your environment.

    Then, use standard SQLCipher for Android to create an empty-but-encrypted database.

    Next, you will need to implement the code to copy the data out of the packaged-but-unencrypted database and insert it into the empty-but-encrypted database.

    Once that is all done, you can close and delete the packaged-but-unencrypted database and just use the encrypted one.

    This might make a useful extension to SQLiteAssetHelper, someday...

    0 讨论(0)
  • 2020-12-09 23:55

    They cover how to use SQLCipher in detail on their website here

    Basically you download their binaries, set them up in your project and then use their SQLiteDatabase class instead of the standard android SQLiteDatabase class.:

     import info.guardianproject.database.sqlcipher.SQLiteDatabase;
    
    0 讨论(0)
提交回复
热议问题