Using SQLCipher to encrypt sqlite db (for iOS)

前端之家收集整理的这篇文章主要介绍了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 exportmy 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.
原文链接:https://www.f2er.com/sqlite/201141.html

猜你在找的Sqlite相关文章