Android 数据库加密

一 简介    

        SQLite是一个轻量的、跨平台的、开源的数据库引擎,它的读写效率、资源消耗总量、延迟时间和整体简单性上具有的优越性,使其成为移动平台数据库的最佳解决方案(如Android、iOS)。Android系统内置了SQLite数据库,并且提供了一整套的API用于对数据库进行增删改查操作,具体就不详细说明了。

       然而,Android平台自带的SQLite有一个致命的缺陷:不支持加密。这就导致存储在SQLite中的数据可以被任何人用任何文本编辑器查看到。如果是普通的数据还好,但是当涉及到一些账号密码,或者聊天内容的时候,我们的应用就会面临严重的安全漏洞隐患。


二 解决方案

1.SQLite加密方式
对数据库加密的思路有两种:
将内容加密后再写入数据库
       这种方式使用简单,在入库/出库只需要将字段做对应的加解密操作即可,一定程度上解决了将数据赤裸裸暴露的问题。
不过这种方式并不是彻底的加密,因为数据库的表结构等信息还是能被查看到。另外写入数据库的内容加密后,搜索也是个问题。
对数据库文件加密
       将整个数据库整个文件加密,这种方式基本上能解决数据库的信息安全问题。目前已有的SQLite加密基本都是通过这种方式实现的。


2.SQLite加密工具

       今天我们要说的是一款开源的SQLite加密工具 SQLCipherSQLCipher是完全开源的,其代码托管在github上。

       SQLCipher使用256-bit AES加密,由于其基于免费版的SQLite,主要的加密接口和SQLite是相同的,但也增加了一些自己的接口。事实上SQLite有加解密接口,只是免费版本没有实现而已。

      SQLCipher分为Community Edition 和 Commercial Edition,前者是免费的,关于  SQLCipher Features 可以参看这里

      关于跨平台支持,官方说明如下:

SQLCipher has broad platform support for with C/C++, Obj-C, QT, Win32/.NET, Java, Python, Ruby, Linux, Mac OS X, iPhone/iOS, Android, Xamarin.iOS, and Xamarin.Android(如iOS、Android)。

       同时支持 Android、iOS 两大平台。


3.SQLCipher集成

      SQLCipher官方提供了详细的集成说明文档,具体参看这里

下面通过一个简单的示例演示如何快速集成SQLCipher到我们的项目中。

3.1 下载官方二进制文件包

      下载地址:https://s3.amazonaws.com/sqlcipher/3.2.0/sqlcipher-for-android-community-v3.2.0.zip

3.2 导入依赖文件

      将下载的后的压缩包解压,解压后如下所示:



将libs 和 assets目录下的所有文件拷贝到我们当前的工程中来,拷贝完成后如下:



3.3 操作数据库

      首先,自定义MySQLiteOpenHelper 继承自 net.sqlcipher.database.SQLiteOpenHelper类,而不是android.database.sqlite.SQLiteOpenHelper,切记!示例代码如下:

package com.ricky.android.sqlitecipher.db;

import com.ricky.android.sqlitecipher.util.Logger;
import android.content.Context;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteDatabase.CursorFactory;
import net.sqlcipher.database.SQLiteOpenHelper;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
	private static final String DB_NAME = "test.db";
	private static final int DB_VERSION = 3;
	
	public MySQLiteOpenHelper(Context context){
		super(context, DB_NAME, null, DB_VERSION);
	}
	
	public MySQLiteOpenHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
		Logger.e("MySQLiteOpenHelper", "onCreate db name="+DB_NAME+" version="+DB_VERSION);
		
		db.execSQL("CREATE TABLE student(id INTEGER PRIMARY KEY AUTOINCREMENT, name text, age integer)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

}


       然后在我们的DAO类中使用 SQLiteDatabase操作数据库。注意,此处是net.sqlcipher.database.SQLiteDatabase,而不是android.database.sqlite.SQLiteDatabase,千万不要引错包了!

package com.ricky.android.sqlitecipher.dao;

import java.util.ArrayList;
import java.util.List;
import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
import android.content.ContentValues;
import android.content.Context;
import com.ricky.android.sqlitecipher.db.SQLiteHelperFactory;
import com.ricky.android.sqlitecipher.model.Student;

public class StudentDAOImpl implements StudentDAO {
	
    private SQLiteOpenHelper sqLiteOpenHelper;
    private String password = "ricky";
    
	public StudentDAOImpl(Context context){
		sqLiteOpenHelper = SQLiteHelperFactory.create(context);
	}
	
	@Override
	public long insert(Student stu) {
		
		SQLiteDatabase db = null;
		try{
			db = sqLiteOpenHelper.getWritableDatabase(password);
			
			ContentValues values = new ContentValues();  
            values.put("name", "Ricky");  
            values.put("age", 24);  
            
			return db.insert("student", null, values);
			
		}finally{
			if(db!=null)
				db.close();
		}
	}

	@Override
	public List<Student> query() {
		
		SQLiteDatabase db = null;
		Cursor cursor = null;
		try{
			db = sqLiteOpenHelper.getWritableDatabase(password);
            
			cursor = db.query("student", new String[]{"id","name","age"}, null, 
					null, null, null, null);
			
			List<Student> list = new ArrayList<>();
			while(cursor!=null && cursor.moveToNext()){
				
				Student stu = new Student();
				stu.setId(cursor.getInt(0));
				stu.setName(cursor.getString(1));
				stu.setAge(cursor.getInt(2));
				
				list.add(stu);
			}
				
			return list;
			
		}finally{
			if(cursor!=null){
				cursor.close();
			}
			if(db!=null)
				db.close();
		}
	}

}

到这里数据的crud基本上实现了,但是还需注意一点: 必须先调用SQLiteDatabase.loadLibs(context);然后再执行数据库相关的操作。

       

       为了方便管理,我单独写了一个 SQLiteHelperFactory类来负责SQLiteOpenHelper的创建,在创建MySQLiteOpenHelper对象之后将调用SQLiteDatabase.loadLibs(context);,代码如下:

package com.ricky.android.sqlitecipher.db;

import com.ricky.android.sqlitecipher.util.Logger;

import android.content.Context;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;

/**
 * SQLiteOpenHelper 工厂
 * @author Ricky
 *
 */
public class SQLiteHelperFactory {
	
	private static final String TAG = SQLiteHelperFactory.class.getSimpleName();
	
	private static SQLiteOpenHelper sqLiteOpenHelper;
	
	private SQLiteHelperFactory(){
		
	}
	
	public static SQLiteOpenHelper create(Context context){
		
		if(sqLiteOpenHelper==null){
			
			synchronized (SQLiteHelperFactory.class) {
				
				if(sqLiteOpenHelper==null){
					
					Logger.e(TAG, "init SQLiteOpenHelper");
					sqLiteOpenHelper = new MySQLiteOpenHelper(context.getApplicationContext());
					
					Logger.e(TAG, "SQLiteDatabase loadLibs");
					//必须先调用此方法
					SQLiteDatabase.loadLibs(context);
				}
			}
		}
		return sqLiteOpenHelper;
	}
}


最后是 MainActivity类

package com.ricky.android.sqlitecipher;

import java.util.List;

import com.ricky.android.sqlitecipher.dao.StudentDAO;
import com.ricky.android.sqlitecipher.dao.StudentDAOImpl;
import com.ricky.android.sqlitecipher.model.Student;
import com.ricky.android.sqlitecipher.util.Logger;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private static final String TAG = MainActivity.class.getSimpleName();
	
	private Button bt_insert;
	private Button bt_query;
	private StudentDAO studentDAO;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		findViewById();
		setListener();
		processLogic();
	}

	private void findViewById() {
		
		bt_insert = (Button) findViewById(R.id.bt_insert);
		bt_query = (Button) findViewById(R.id.bt_query);
	}

	private void setListener() {
		bt_insert.setOnClickListener(this);
		bt_query.setOnClickListener(this);
	}

	private void processLogic() {
		
		studentDAO = new StudentDAOImpl(this);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_insert:
			
			Student stu = new Student();
			stu.setName("Mike");
			stu.setAge(24);
			
			long id = studentDAO.insert(stu);
			
			Logger.i(TAG, "insert id="+id);
			
			break;

		case R.id.bt_query:
			
			List<Student> list = studentDAO.query();
			if(list!=null){
				Logger.i(TAG, "student list size="+list.size());
			}else{
				Logger.i(TAG, "student list is empty");
			}
			
			break;
			
		default:
			break;
		}
	}
}



OK,关于SQLCipher的集成到这里就大功告成啦,最后另附Demo源码(下载地址见文章末尾),有问题的话可以留言进行交流咯!





Demo下载地址:http://download.csdn.net/detail/fx_sky/8165223




  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
是的,GreenDAO支持数据库加密。你可以通过在DaoMaster类中使用SQLCipher来加密数据库。SQLCipher是一个基于SQLite的开源库,可用于加密SQLite数据库。 以下是使用SQLCipher加密GreenDAO数据库的步骤: 1. 在build.gradle文件中添加SQLCipher库的依赖: ``` dependencies { implementation 'net.zetetic:android-database-sqlcipher:4.4.0' } ``` 2. 在DaoMaster类中使用SQLCipher初始化数据库。你需要在使用SQLiteOpenHelper创建数据库之前,调用SQLiteDatabase.loadLibs(context)加载SQLCipher库,并在创建数据库时,指定加密密码。 ```java public class MyDaoMaster extends DaoMaster.OpenHelper { public MyDaoMaster(Context context, String name) { super(context, name); SQLiteDatabase.loadLibs(context); } public MyDaoMaster(Context context, String name, SQLiteDatabase.CursorFactory factory) { super(context, name, factory); SQLiteDatabase.loadLibs(context); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { super.onUpgrade(db, oldVersion, newVersion); // 数据库升级时,需要重新加密数据 db.execSQL("PRAGMA cipher_migrate;"); } @Override public SQLiteDatabase getWritableDatabase() { SQLiteDatabase db = super.getWritableDatabase(SQLCipherUtils.getEncryptionKey()); // 开启数据库加密 db.rawExecSQL("PRAGMA key = '"+SQLCipherUtils.getEncryptionKey()+"';"); db.rawExecSQL("PRAGMA cipher_use_hmac = off;"); db.rawExecSQL("PRAGMA cipher_page_size = 1024;"); db.rawExecSQL("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;"); db.rawExecSQL("PRAGMA cipher_hmac_algorithm = HMAC_SHA1;"); return db; } } ``` 3. 在Application类中初始化GreenDAO。你需要使用自定义的MyDaoMaster类,以及指定数据库名称和版本号。 ```java public class MyApplication extends Application { private DaoSession daoSession; @Override public void onCreate() { super.onCreate(); // 初始化GreenDAO DaoMaster.DevOpenHelper helper = new MyDaoMaster(this, "my-db"); Database db = helper.getEncryptedWritableDb(SQLCipherUtils.getEncryptionKey()); daoSession = new DaoMaster(db).newSession(); } public DaoSession getDaoSession() { return daoSession; } } ``` 需要注意的是,加密密码需要妥善保管,不要轻易泄露。你可以通过SQLCipherUtils类来生成加密密码,以及验证密码是否正确。 ```java public class SQLCipherUtils { private static final String KEY = "my-secret-key"; public static String getEncryptionKey() { return KEY; } public static boolean isPasswordCorrect(String password) { return password != null && password.equals(KEY); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值