Android使用SQLCipher对SQLite数据库进行加密
来源:互联网 发布:it服务管理平台 编辑:程序博客网 时间:2024/06/09 22:41
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";@Overrideprotected 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_KEYmSqLiteDatabase = mSqlCipherOpenHelper.getWritableDatabase(SECRET_KEY); }private class ClickListenerImpl implements OnClickListener {private Person person;@Overridepublic 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);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20),phone VARCHAR(12))");}@Overridepublic 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;}@Overridepublic 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>
- Android使用SQLCipher对SQLite数据库进行加密
- [Android初级]使用Sqlcipher对sqlite数据库进行加解密
- 使用SQLCipher 对sqlite数据库进行解密
- 使用SQLCipher对数据库加密
- Android中对sqlite加密--SQLCipher
- SQLCipher加密SQLite数据库
- Android加密已有的sqlite数据库---sqlcipher
- Android SQLite加密--SQLCipher
- Android 使用Sqlcipher给Sqlite加密
- ios使用sqlcipher对sql进行加密
- 用sqlcipher对已有的SQLite数据库加密
- Sqlite数据库加密框架 sqlcipher
- sqlite 数据库加密(SQLCipher)
- sqlite 数据库加密(SQLCipher)
- Android使用SQLCipher对数据库文件加密
- Android使用SQLCipher对加密数据库文件解密
- android 加密数据库android-database-sqlcipher使用
- 正确使用SQLCipher来加密Android数据库
- Ubuntu系统下使用rxtx开发串口程序
- Android 数据库升级解决方案
- 助人创业的创业故事
- seaJs学习笔记之seaJs的准备
- Apache Tomcat配置gzip压缩(HTTP压缩)功能
- Android使用SQLCipher对SQLite数据库进行加密
- 博客写作之我说
- CKFinder2.1的破解方法
- .Java中的异常、断言、日志【草稿下,Log4j专题】
- jQuery Ajax 实例 全解析
- 办假机动车登记証书Q【983001888】记者証
- 直方图与匹配 - 直方图的计算与显示(cvCalcHist)
- background-position 用法详细介绍
- 项目中用到的设计模式:Reactor设计模式