How to encrypt / decrypt in SQLCipher

I am struggling with How to encrypt / decrypt a DB using SQLCipher.

In my this code, where should I start from. and how to start.

Please change this code if possible.

I followed this link but didn't figure out how to do it. http://sqlcipher.net/sqlcipher-api/#key

My DB file: -

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
import android.content.Context;
import android.util.Log;

public class DatabaseClass extends SQLiteOpenHelper 
{
    /**
     * Constants 
     */
    public final static String DATABASE_NAME ="mycipherdatabase.db";
    public final static String NAME ="name";
    public final static String ADDRESS ="address";
    public final static String CITY ="city";

    public static String pass = "1234";
    public DatabaseClass(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    /**
     * called once
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL( "CREATE TABLE mylistdata(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, address TEXT,city TEXT);");

        Log.v("inside", "oncreate");
    }

    /**
     * called when we upgrade(change)the version number of database
     * onCreate also called after changing the version
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP Table mylistdata");
        onCreate(db);
    }
}

      

OtherActivity Using this DB: -

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SQLiteDatabase.loadLibs(this);
        File file = getDatabasePath(DatabaseClass.DATABASE_NAME);
        file.mkdir();
        file.delete();
        sqldb = SQLiteDatabase.openOrCreateDatabase(file,key, null);
        db = new DatabaseClass(this);

        etname = (EditText)findViewById(R.id.name);
        etadd = (EditText)findViewById(R.id.add);
        etcity = (EditText)findViewById(R.id.city);
        result = (ListView)findViewById(R.id.mylist);
    }

    /**
     *insert data in to database 
     */
    public void saveData(View v) {
        ContentValues insertData = new ContentValues();
        insertData.put(DatabaseClass.NAME, etname.getText().toString());
        insertData.put(DatabaseClass.ADDRESS, etadd.getText().toString());
        insertData.put(DatabaseClass.CITY, etcity.getText().toString());

        db.getWritableDatabase(key).insert("mylistdata", DatabaseClass.NAME , insertData);
        db.close();

        resetEditText();
        Toast.makeText(this,"Data saved", Toast.LENGTH_SHORT).show();
    }

      

+3


source to share


3 answers


Well, there are so many problems with the code that I think you should have a better look at the SQLite framework in Android, technically Android SQLite and SQLCipher . exactly the same (except for the password). so if you are writing a standard SQLite application it can be easily changed to SQLCipher,

I suggest you read this excellent article on SQLite in android.

http://www.vogella.com/articles/AndroidSQLite/article.html

Tip : no need to call openOrCreateDatabase when you are using SQLiteOpenHelper , call getWritableDatabase will do the job, also you must have SQLiteDatabase object taken from getWritableDatabase method , calling getWritableDatabase every time you want to insert record is wrong.

these parts will be removed,



     // in onCreate method
     File file = getDatabasePath(DatabaseClass.DATABASE_NAME);
     file.mkdir();
     file.delete();
     sqldb = SQLiteDatabase.openOrCreateDatabase(file,key, null);
     db = new DatabaseClass(this);

     // in saveData method
      db.getWritableDatabase(key).insert("mylistdata", DatabaseClass.NAME ,               insertData);

      

Instead, you should do something like this

     // in onCreate method
     try{
        sqldb = new DatabaseClass(this).getWritableDatabase(key);
     }catch(Throwable e){
        // error occurred
     }

     // in insert action
     try{
        sqldb.insert("tablename", null, contentValues);
     }catch(Throwable e){
        // error occurred
     }

      

This way you can also see if an error has occurred, Remember not to use the Exception class in the catch clause, only Throwable !

Also, I would not suggest to close the connection for every insert action, better take care of the higher levels

+4


source


Try using SQLiteDatabase.openOrCreateDatabase as described here: http://sqlcipher.net/sqlcipher-for-android/



+1


source


This might be a good place to start, https://guardianproject.info/code/sqlcipher/

And more from Android docs, http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onConfigure(android.database.sqlite.SQLiteDatabase)

The key takeaways for me were adding the sqlcipher jar to your android defrags and make sure you set the PRAGMA stmts secret keys / passphrases.

0


source







All Articles