Android使用SQLCipher对SQLite数据库进行加密

简介: MainActivity如下: package cc.testsqlcipher;import net.sqlcipher.Cursor;import net.

MainActivity如下:

package cc.testsqlcipher;

import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
/**
 * Demo描述:
 * 使用SQLCipher对SQLite数据库进行加密
 * 
 * 参考资料:
 * 1 http://blog.csdn.net/guolin_blog/article/details/11952409
 * 2 http://blog.csdn.net/zhuawami/article/details/9038003
 *   Thank you very much
 */
public class MainActivity extends Activity {
	private Button mAddButton;
    private Button mQueryButton;
    private SQLiteDatabase mSqLiteDatabase;
    private SQLCipherOpenHelper mSqlCipherOpenHelper;
    private final String SECRET_KEY="95279527";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initView();
		initDataBase();
	}

	private void initView(){
		mAddButton=(Button) findViewById(R.id.addButton);
		mAddButton.setOnClickListener(new ClickListenerImpl());
		mQueryButton=(Button) findViewById(R.id.queryButton);
		mQueryButton.setOnClickListener(new ClickListenerImpl());
	}
	
	private void initDataBase() {
		//加载libs/armeabi中的so
        SQLiteDatabase.loadLibs(this);
        //获取到SQLiteOpenHelper
        mSqlCipherOpenHelper=new SQLCipherOpenHelper(this);
        //设置打开数据库的密码SECRET_KEY
		mSqLiteDatabase = mSqlCipherOpenHelper.getWritableDatabase(SECRET_KEY);  
	}
	
	private class ClickListenerImpl implements OnClickListener {
		private Person person;
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.addButton:
				for (int i = 0; i < 15; i++) {
					person = new Person("xiaoming" + i, "9527" + i);
					addData(person);
				}
				break;
			case R.id.queryButton:
                person=queryData(8);
                System.out.println(""+person.toString());
				break;
			default:
				break;
			}
		}

	}
	
	public void addData(Person person) {
		mSqLiteDatabase.execSQL("insert into person (name,phone) values(?,?)",
				                 new Object[] {person.getName(), person.getPhone() });

	}
	
	public Person queryData(int id){
		Cursor cursor=mSqLiteDatabase.rawQuery("select * from person where personid=?", 
				                                new String[]{String.valueOf(id)});
		while(cursor.moveToFirst()){
			int personid=cursor.getInt(cursor.getColumnIndex("personid"));
			String name=cursor.getString(cursor.getColumnIndex("name"));
			String phone=cursor.getString(cursor.getColumnIndex("phone"));
			return new Person(personid, name, phone);
		}
		cursor.close();
		return null;
	}


}


SQLCipherOpenHelper如下:

package cc.testsqlcipher;

import android.content.Context;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
/**
 * 注意:
 * 这里包的引用,均是在:
 * net.sqlcipher.database之下
 */
public class SQLCipherOpenHelper extends SQLiteOpenHelper {
    private final static String DATABASE_NAME="test.db";
	public SQLCipherOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20),phone VARCHAR(12))");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

}


 

Person如下:

package cc.testsqlcipher;
public class Person {
	private Integer id;
	private String name;
	private String phone;
	
	public Person(String name, String phone) {
		this.name = name;
		this.phone = phone;
	}
	public Person(Integer id, String name, String phone) {
		this.id = id;
		this.name = name;
		this.phone = phone;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", phone=" + phone + "]";
	}
	
}


 

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用SQLCipher对数据库进行加密" 
        android:layout_centerHorizontal="true"
    />
    
     <Button
        android:id="@+id/addButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="添加数据"
        android:layout_marginTop="100dip"
       />
     
      <Button
        android:id="@+id/queryButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="查找数据"
        android:layout_marginTop="200dip"
       />


</RelativeLayout>


 

相关文章
|
7天前
|
监控 安全 算法
矢量数据库安全性:数据加密与访问控制
【4月更文挑战第30天】本文探讨了矢量数据库的安全性,聚焦数据加密和访问控制。数据加密,包括选择安全、高效的算法,字段级加密及传输加密,保护敏感信息。访问控制涉及用户认证、权限管理和审计监控,确保合法用户访问。安全性的提升需要持续投入,关注新技术和安全威胁,以适应不断变化的环境。
|
8天前
|
SQL 存储 Ubuntu
在ubuntu中将dict.txt导入到数据库sqlite3
这样,你就成功将 `dict.txt` 中的数据导入到名为 `mydatabase.db` 的SQLite3数据库中的 `words` 表格中了。请根据实际情况调整表格结构和数据导入命令。
14 0
|
10天前
|
存储 Java 数据库
SpringBoot使用jasypt实现数据库配置加密
这样,你就成功地使用Jasypt实现了Spring Boot中的数据库配置加密,确保敏感信息在配置文件中以加密形式存储,并在应用启动时自动解密。
40 2
|
19天前
|
SQL 数据库 数据库管理
Python数据库操作(SQLAlchemy、SQLite等)面试题集
【4月更文挑战第15天】本文介绍了Python数据库操作的面试重点,涵盖SQLAlchemy ORM和SQLite。内容包括:1) 使用SQLAlchemy定义SQLite表的Python类及执行CRUD操作,强调ORM使用和会话管理;2) 查询优化与性能,涉及JOIN、分组、聚合查询,并提醒注意懒加载和索引创建;3) 异常处理和事务管理,展示如何捕获异常并进行事务控制。通过理解这些知识点并避免常见错误,可在面试中表现出色。
21 0
|
23天前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
28天前
|
关系型数据库 MySQL 数据库连接
Python+SQLite数据库实现服务端高并发写入
Python中使用SQLite内存模式实现高并发写入:创建内存数据库连接,建立表格,通过多线程并发写入数据。虽然能避免数据竞争,但由于SQLite内存模式采用锁机制,可能在高并发时引发性能瓶颈。若需更高性能,可选择MySQL或PostgreSQL。
34 0
|
1月前
|
Java 数据库 数据安全/隐私保护
SpringBoot项目使用jasypt加解密的方法加密数据库密码
SpringBoot项目使用jasypt加解密的方法加密数据库密码
9 0
|
1月前
|
存储 安全 数据库
数据安全之认识数据库加密系统
信息安全的关键在于数据的安全,而数据的安全则主要通过数据加密技术来实现。随着网上购物等电子商务的兴起和繁荣,以数据库为代表的信息安全已成为很多企业的共识。越来越多的企业和机构开始重视数据库的数据安全问题,因为一旦数据泄露或遭到非法访问,将可能导致严重的经济损失和声誉损害。为了增强普通关系数据库管理系统的安全性,数据库加密系统应运而生。
19 0
|
Java 数据安全/隐私保护
Java实现最电话号码的简单加密源码
Java实现最电话号码的简单加密源码
20 0
|
3月前
|
存储 安全 算法
【接口加密】Java中的接口加密实践
【接口加密】Java中的接口加密实践