sqlite3加密方案sqlcipher,及sqlcipher使用指南

前端之家收集整理的这篇文章主要介绍了sqlite3加密方案sqlcipher,及sqlcipher使用指南前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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就可以了

我的个人博客程序猿日记——王德新的个人博客

原文链接:https://www.f2er.com/sqlite/199910.html

猜你在找的Sqlite相关文章