Question

I need to use SQLCipher for android...i've already made my app using SQLite and want to just convert it to SQLCipher.

The problem is, I know nothing about SQLCipher.

I have read about it in this link: http://sqlcipher.net/sqlcipher-for-android/

But i'm not too clear, still. I was wondering if you could provide some basic sqlcipher for android tutorials, where everything is taught in an easy way from the absolute basics.

Thanks!

Was it helpful?

Solution

To properly use SQL Cipher for Android you must use external libraries and change some of the code which interacts with your DB.

  1. These must first be added to your project (within the libs folder.) Refer here to get these: http://sqlcipher.net/sqlcipher-for-android/

  2. Secondly you need to add the icudt4dl.zip file to your assets folder, this Zip comes with the SQL Cipher libraries.

  3. Right click your project, go to properties then Java build path then include libraries such as commons-codec.jar, guava-r09.jar, sqlcipher.jar. Once this is done, do a build clean.

  4. Then within your App, instead of importing android.database.sqlite, you will import import net.sqlcipher.database

  5. Change any code which interacts with the DB, example:

    SQLiteDatabase.loadLibs(context);

    String dbPath = this.getDatabasePath("dbname.db").getPath();

    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath,"dbPassword", null);

  6. Verify that the database is encrypted, go to DDMS perspective in Eclipse, click the file explorer tab, navigate to data/data/, click on the .db file and select get device file, save it to your desktop and open it with a text editor. Look for the plain text values you have been inserting into your database, if you can still read them, something has gone wrong.

It might also be a good idea to check out some SQLite tutorials before implementing SQL Cipher. A good one is mentioned here: Android sqlite database - where do i start as the tutorial has gone for notepad?

Update

This answer is outdated now and Eclipse is practically deprecated for Android Development. I recently had to build an app using SQLCipher on Android Studio for Android 5 + 6 and these are the steps I followed.

In Android Studio, you can include SQLCipher as a dependency in your build file. Update your dependencies in build gradle to include the following line:

dependencies{
    compile 'net.zetetic:android-database-sqlcipher:3.5.4@aar'
}

You can keep up to date with versions here: https://mvnrepository.com/artifact/net.zetetic/android-database-sqlcipher

My App wouldn't build unless I removed the SQLCipher files in the lib folder and the asset folder, but after that it worked as expected. Once you made these changes, run a build/clean and check if it works.

The same steps mentioned above with changing your code still stand.

OTHER TIPS

While you can still follow Zetetic's Eclipse tutorial and add .so libraries yourself, you really don't need to do it in Android Studio. Just add a Gradle dependency, like compile net.zetetic:android-database-sqlcipher:3.3.1-2@aar and you're ready to go!

Here you can always check the latest aar version and here you can read more about integration.

//in build.gradle
implementation 'net.zetetic:android-database-sqlcipher:4.4.0@aar'
implementation "androidx.sqlite:sqlite:2.1.0"

 // in activity
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // initialize sqlite libraries
        SQLiteDatabase.loadLibs(this)

        // initializing db
        val database = SQLiteDatabase.openOrCreateDatabase(
            "/some/path",
            "mypassword",
            null
        )

        // creating table if not exists
        database.execSQL("create table if not exists $DB_TABLE_NAME($DB_COLUMN1_NAME,  $DB_COLUMN2_SURNAME)")

        // get data from db
        s = loadFromDB()
    }




  private fun loadFromDb(): String {
        var s = ""
        var i = 0

        createTableIfNotExist()
        val cursor = database.rawQuery("select * from $DB_TABLE_NAME", null)
        cursor?.moveToFirst()
        if (!cursor.isAfterLast) {
            do {
                s += "col_${i++}: name:${cursor.getString(0)} surname:${cursor.getString(1)}\n"
            } while (cursor.moveToNext())

            cursor.close()
            return s
        }

        return s
    }

Getting started is a bit difficult without a real world example and documentation! Here is a simple example and documentation. It is very simple and convenient to use!

If you like the example, don't forget to star and contribute if you want). Thank you!

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