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.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值