Android Sqlite数据库加密
Android使用的是开源的SQLite数据库,数据库本身没有加密,加密思路通常有两个:
1. 对几个关键的字段使用加密算法,再存入数据库
2. 对整个数据库进行加密
SQLite数据库加密工具:
收费工具:
免费工具:
SQLCipher使用:
SQLCipher是完全开源的软件,提供256-bit AES加密
源码编译:
1. OpenSSL编译
SQLCipher源码编译需要依赖OpenSSL提供的libcrypto
下载OpenSSL源码,这里选择稳定版本1.0.1h
openssl-1.0.1h Admin$ ./config --prefix=/usr/local --openssldir=/usr/local/openssl
openssl-1.0.1h Admin$ make
openssl-1.0.1h Admin$ make test
openssl-1.0.1h Admin$ make install
2. SQLCipher源码编译
下载地址:https://github.com/sqlcipher/sqlcipher
sqlcipher Admin$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="/usr/local/lib/libcrypto.a"
sqlcipher Admin$ make
命令行使用:
1. 创建加密数据库
$ sqlcipher encrypted.db
SQLCipher version 3.8.4.3 -- ::
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA key = 'thisiskey';
sqlite> create table encrypted (id integer, name text);
sqlite> .schema
CREATE TABLE encrypted (id integer, name text);
sqlite> .q
2. 打开加密数据库
$ sqlcipher encrypted.db
SQLCipher version 3.8.4.3 -- ::
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA key = 'thisiskey';
sqlite> .schema
CREATE TABLE encrypted (id integer, name text);
3. 修改数据库密码
sqlite> PRAGMA rekey = 'newkey';
4. 加密已有的数据库
$ sqlcipher banklist.sqlite3
SQLCipher version 3.8.4.3 -- ::
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'thisiskey';
sqlite> SELECT sqlcipher_export('encrypted');
sqlite> DETACH DATABASE encrypted;
5. 解密数据库
$ sqlcipher encrypted.db
SQLCipher version 3.8.4.3 -- ::
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA key = 'thisiskey';
sqlite> ATTACH DATABASE 'plaintext.db' AS plaintext KEY '';
sqlite> SELECT sqlcipher_export('plaintext');
sqlite> DETACH DATABASE plaintext;
Android版本SQLCipher使用
android版本源码,编译需要依赖的东西很多,懒得去试了,可以直接下载已经编译好的binary,官网下载,或者这里为3.1.0版本
注:github上的binary为2.1.1,实际测试在Android 4.4 kitkat上无法使用,请从官网下载最新的3.1.0版本
1. 将解压后的libs和asserts添加到工程:

2. 将工程中原有的android.database.sqlite.*全部替换为net.sqlcipher.database.*,原先的android.database.Cursor可以保留

3. 在activity或者其他调用数据库的地方,注意要在使用数据库之前加上:
SQLiteDatabase.loadLibs(this);
备注:使用SQLCipher命令行将原先的数据库加密之后,新数据库的version有可能为0,导致在SQLiteOpenHelper中会进入到onCreate()中,重建数据库。
解决方法,将数据库版本改回原先的版本:
sqlite> PRAGMA user_version = ;
Android Sqlite数据库加密的更多相关文章
- Android Sqlite 数据库版本更新
Android Sqlite 数据库版本更新 http://87426628.blog.163.com/blog/static/6069361820131069485844/ 1.自己写一个类继承 ...
- Android SQLite 数据库详细介绍
Android SQLite 数据库详细介绍 我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用 ...
- Android sqlite数据库存取图片信息
Android sqlite数据库存取图片信息 存储图片:bitmap private byte[] getIconData(Bitmap bitmap){ int size = bitmap.get ...
- Android SQLite 数据库 增删改查操作
Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...
- 图解IntelliJ IDEA 13版本对Android SQLite数据库的支持
IntelliJ IDEA 13版本的重要构建之一是支持Android程序开发.当然对Android SQLite数据库的支持也就成为了Android开发者对IntelliJ IDEA 13版本的绝对 ...
- c# sqlite 数据库加密
c# sqlite 数据库加密 2010-05-29 10:55 用了ADO.NET 2.0 SQLite Data Provider这样可以直接利用它来创建一个加密的sqlite数据库.有关c#代码 ...
- Android——SQLite/数据库 相关知识总结贴
android SQLite简介 http://www.apkbus.com/android-1780-1-1.html Android SQLite基础 http://www.apkbus.com/ ...
- Android SQLite数据库使用
在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的.一. ...
- android: SQLite 数据库的最佳实践
6.5.1 使用事务 前面我们已经知道,SQLite 数据库是支持事务的,事务的特性可以保证让某一系列的操 作要么全部完成,要么一个都不会完成.那么在什么情况下才需要使用事务呢?想象以下场 景, ...
随机推荐
- 学习日记-发布第一篇WordPress
配置WordPress 1.安装XAMPP https://www.apachefriends.org/zh_cn/index.html 2.下载最新版WordPress 解压至XAMPP安装路径下. ...
- ThinkPHP模板中如何操作session,以及如果session中保存的是数组的情况
在ThinkPHP的模板中操作session时,可以参考ThinkPHP参考文档中的“模板—>系统变量”部分,在默认模板引擎中,语法如下: {$Think.session.user} //输出s ...
- 初学者的python学习笔记1
推荐一段时间闲的蛋疼,总觉得再堕落下去不太好,便捡起了之前一直想学而没有学的python,以此记录一下学习笔记,同时亦是督促和复习. 学习51cto上的<2016最新Python开发基础课程-2 ...
- xcode6 使用pch出错解决办法
1down vote If you decide to add a .pch file manually and you want to use Objective-C just like befor ...
- python实现的视频下载工具you-get,支持多个国内外主流视频平台
RT,you-get 是一个视频离线下载工具, https://github.com/soimort/you-get 另一个同类工具 youtube-dl 也是python 实现,虽然名为 youtu ...
- Scrum领取任务
这次主要讨论了产品的构造流程,怎么将任务分配到个人,讨论什么功能具体怎么实现,然后各自选取了任务. 在团队项目“广商百货”的SCRUM项目中我认领的任务是对登录功能的实现.现在还没正式开始,还在看书和 ...
- 【转】 Linux shell的&&和||
http://www.2cto.com/os/201302/189655.html Linux shell的&&和|| shell 在执行某个命令的时候,会返回一个返回值,该返回值 ...
- C#中Dictionary<TKey,TValue>排序方式
自定义类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...
- 理解python的with语句
Python’s with statement provides a very convenient way of dealing with the situation where you have ...
- MFC之TreeCtrl控件使用经验总结
树形控件可以用于树形的结构,其中有一个根接点(Root)然后下面有许多子结点,而每个子结点上有允许有一个或多个或没有子结点.MFC中使用CTreeCtrl类来封装树形控件的各种操作.通过调用BOOL ...