您的位置:首页 > 移动开发 > Android开发

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

2013-10-28 15:33 471 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: