sqlite是一款开源的轻量级数据库,现在android和ios都在使用它来存储结构化数据,但是加密版的并非开源。折中一下只能找开源的解决方案,sqlcipher是一个不错的选择,它可以对sqlite实现加密,并且有在android和ios都有相应的库进行解密读取,只是引入的库会增加app的大小。SQLCipher is an open source library that provides transparent,secure 256-bit AES encryption of SQLite database files. 1.下载源代码 官方源代码:https://github.com/sqlcipher/sqlcipher 2、相关的依赖 安装openssl及tcl,这两个依赖必须安装,不然就会很悲剧 openssl 使用yum openssl-devel 安装,tcl使用yum tcl-devel安装 3、编译 ./configure –enable-tempstore=yes CFLAGS=”-DSQLITE_HAS_CODEC” LDFLAGS=”-lcrypto” make 4.验证编译是否成功 创建一个加密的数据,密码是aaa:
$ sqlcipher test.sqlite SQLite version 3.7.14.1 2012-10-04 19:37:12 Enter “.help” for instructions Enter SQL statements terminated with a “;” sqlite> PRAGMA key = ‘aaa’; sqlite> create table a(ind int); sqlite> .tables a sqlite> .quit 尝试不输入密码,直接读取数据库,理论上是读不到数据,或者报错:
$ sqlcipher test.sqlite SQLite version 3.7.14.1 2012-10-04 19:37:12 Enter “.help” for instructions Enter SQL statements terminated with a “;” sqlite> .tables sqlite> .quit 尝试正确输入密码,应该成功读取:
$ sqlcipher test.sqlite SQLite version 3.7.14.1 2012-10-04 19:37:12 Enter “.help” for instructions Enter SQL statements terminated with a “;” sqlite> PRAGMA key = ‘aaa’; sqlite> .tables a sqlite> .quit
给现有数据进行加密 如何给现有的sqlite文件进行加密,没有别的简单的方法:
1.先把数据导出:
$ sqlite3 ifood.sqlite >.output ifood.sql >.dump 2.创建一个新的加密的数据库:
$ sqlcipher ifood_lock.sqlite sqlite> PRAGMA key = ‘abcdef’; # 设置密码 3.导入数据
>.read ifood.sql
SQLCipher For Android https://github.com/sqlcipher/sqlcipher 这是源码需要编译,比较麻烦,可以到http://download.csdn.net/detail/wdxin1322/8378519
使用很简单,可以参照 https://www.zetetic.net/sqlcipher/sqlcipher-for-android/,然后将项目中的sqllite 的import改成相应的SQLCipher就可以了
我的个人博客程序猿日记——王德新的个人博客 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|