How to get started with SQLCipher for android?
Asked Answered
F

3

24

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!

Flourishing answered 16/7, 2013 at 8:32 Comment(2)
I did a conference workshop on SQLCipher and related security concerns (slideshare.net/commonsguy/sqlcipher-for-android) and have two chapters in my book on SQLCipher, including a tutorial I used for the hands-on portion of that workshop (commonsware.com/Android).Cecilacecile
Do you have specific questions that you are unclear about?Upsydaisy
C
27

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.

Chemisorb answered 26/4, 2014 at 14:7 Comment(1)
Refer to this post as it more up to date: https://mcmap.net/q/582963/-integrating-sqlcipher-in-android-studio-projectAldana
W
9

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.

Worldly answered 31/3, 2016 at 16:18 Comment(0)
S
0
//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!

Silverfish answered 19/6, 2020 at 15:44 Comment(2)
While these links may answer the question, it is better to include the essential parts of the answer here and provide the links for reference. Link-only answers can become invalid if the linked page changes.Tussle
Thank you for your advice! I will update my answer as soon as possibleSilverfish

© 2022 - 2024 — McMap. All rights reserved.