Using SQLCipher to encrypt sqlite db (for iOS)

1. Follow the steps in  http://sqlcipher.net/ios-tutorial/
      Hints:
      *   DELETE existing sqlite3.* from your source code tree if you were using sqlite code earlier (very likely). SqlCipher is kind of a port from standard sqlite3, so they cannot exist at the same time (they have roughly the same interfaces)
      *   The line " and add "sqlcipher" as a search path" in the tutorial means adding the path for "sqlcipher" project

      That's pretty much for the 1st step. u r good to go.

2. Create SqlCipher commandline tool
      Somehow my previous db cannot be used by sqlcipher. It seems there is no GUI enabled tool for sqcipher. So we need to use command line.
      Go to sqlcipher source and run the following commands:

./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto"
make

      You'll find sqlite3 in your current path. This is the "cipher-enabled" sqlite3, not the default "non-cipher-enabled" sqlite3. So you must run './sqlite xxx.db' to use this one.

3. (Dummy way to export my previous db to encyrpted db)
      My previous db was created by SQLite Database Brower 2.0. Somehow it doesn't work with sqlcipher after I tried encrypting it. Maybe there are other tricks I don't know, but I don't really care. Let's do it in a dummy way:
      -   In SQLite Database brower, open db file, select File->Export->Database to SQL file. Save it as sql.
      - in sqlcipher path, run:
          ./sqlite3 data.db
      -   You need to create an encrypted EMPTY database first. run:
          PRAGMA key='YOUR_PASS';
          PRAGMA cipher='aes-256-cfb';    // by default, it's aes-256-cbc. not much difference. just don't want to use default value
         .q
      - open the db again (./sqlite data.db)

      -  PRAGMA key='YOUR_PASS';
         PRAGMA cipher='aes-256-cfb';
      - run:    .read database.sql
         .q

      - open the db, now u must use key/cipher to decrypt first.

Now back to XCode, you need to use sqlite3_exec(...) to specify the aes mode before running any sql statements.

*   Note:  The F***ing SQLCipher has problem handling armv7/armv7s correctly. On my iPhone4, it will use armv7 setting, but the main project will use armv7s, then it complains "undefined symbol for xxxx".  As mentioned in:
https://github.com/sqlcipher/sqlcipher/issues/44
http://stackoverflow.com/questions/13153439/ios-sqlcipher-sqlite-simulator-ok-deploy-on-device-error

Removing armv7s in the main project makes it working. Well, it only matters when debugging on real device.  I can still unplug my iphone and choose "Archive" to publish to App Store without modifying the Architecture setting . We can just remove "armv7s" for debug build, and leave everything else alone.

你可能感兴趣的:(ios,sqlite3)