Skip to content

Instantly share code, notes, and snippets.

@riyaz-ali
Last active June 28, 2018 15:29
Show Gist options
  • Save riyaz-ali/07c7bb60a9a1e98ba766099777676edf to your computer and use it in GitHub Desktop.
Save riyaz-ali/07c7bb60a9a1e98ba766099777676edf to your computer and use it in GitHub Desktop.

How to use SQLCipher with SQLite on Android?

This Gist was created to demonstrate the solution for SO Answer https://stackoverflow.com/a/44450154/6611700

Steps:

  1. Add SQLCipher dependency in your application's build.gradle file
    compile net.zetetic:android-database-sqlcipher:3.5.7@aar
  2. Initialize SQLCipher's native library before performing any db ops by calling: net.sqlcipher.database.SQLiteDatabase.loadLibs(context)
  3. Then use SQLiteHelper or similar implementation to create and use the encrypted database
/**
* Activity class consuming the encrypted database
*/
package com.riyazali.example
//- other imports
import net.sqlcipher.database.SQLiteDatabase;
//- SQLiteOpenHelper
import com.riyaz.example.SQLHelper
public class MainActivity extends Activity {
// SQLHelper's instance
private SQLHelper mHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// initialize SQLCipher
mHelper = initSQLCipher();
// use the database
doSomeDatabaseOps();
}
/**
* SQLCipher initialization helper
*/
private SQLHelper initSQLCipher() {
// load SQLCipher's native JNI Libs
// do this only once in your application
// probably when launching your app / sql-related operation
SQLiteDatabase.loadLibs(this);
// find more about constructor's signature at -
// https://github.com/sqlcipher/android-database-sqlcipher/blob/master/src/net/sqlcipher/database/SQLiteOpenHelper.java#L62
return new SQLHelper(this, "file.db", null, 1);
}
/**
* Some random db ops
*/
private void doSomeDatabaseOps() throws IllegalStateException, SQLiteException {
// can throw
SQLiteDatabase db = mHelper.getReadableDatabse("PASSWORD"); // same applies for getWriteableDatabse()
// do something with db!!!
db.execSQL("create table t1(a, b)");
db.execSQL("insert into t1(a, b) values(?, ?)", new Object[]{"one for the money", "two for the show"});
}
/**
* Cleanup when done
*/
@Override
public void onStop(){
super.onStop();
// close mHelper
mHelper.close()
}
}
/**
* Example implementation of SQLiteOpenHelper with SQLCipher
*
* Create/inherit your existing SQLiteOpenHelper implementation from net.sqlcipher.database.SQLiteOpenHelper
*/
package com.riyazali.example
//- other imports
import net.sqlcipher.database.SQLiteDatabase; // SQLCipher's implementation (subclass) of Android's SQLiteDatabase to provide encryption related functionalities
import net.sqlcipher.database.SQLiteOpenHelper
public class SQLHelper extends SQLiteOpenHelper {
public void onCreate(SQLiteDatabase db){
// your creation logic
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
// your upgrade logic
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment