Android使用SQLCipher对数据库文件加密

转载地址:http://blog.csdn.net/wshngyf/article/details/51112664


查了好多资料,大多数都是抄来抄去,还有很多文章时间都是几年前的了。根据SQLCipher在github上的公开的资料,做一个教程。

SQLCipher是对数据库文件加密,不是对数据库内容加密。这一点要分清楚。

首先放出SQLCipher的github地址:https://github.com/sqlcipher

第一步:下载SQLCipher在Android上应用的例子,看看他是如何给android的SQLite数据库文件加密的。sqlcipher-android-tests的下载地址: 
https://github.com/sqlcipher/sqlcipher-android-tests 
在assets目录下找到icudt46l.zip包。移植到自己创建的工程中去。还有libs文件夹下的所有文件。这里看到我的工程是不能执行的,这个没有关系,把需要的文件移植到自己创建的工程中去就行。 
这里写图片描述

这里是我新建的工程文件: 
这里写图片描述

第二步:创建自己的工程文件,使用数据库。 
导包的时候注意不要导错! 
sqldatabasehelper.Java

public class sqldatabasehelper extends SQLiteOpenHelper {
    //创建test数据表的sql语句
    public static final String CREATE_TABLE = "create table Test(name text, pd integer)";

    public sqldatabasehelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建数据表
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

    }

}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

MainActivity.java

public class MainActivity extends Activity {
    private SQLiteDatabase db;

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

        SQLiteDatabase.loadLibs(this);
        sqldatabasehelper dbHelper = new sqldatabasehelper(this, "my.db", null, 1);
        //这里的123456是给数据库加密的密码
        db = dbHelper.getWritableDatabase("123456");
        Button addData = (Button) findViewById(R.id.add_data);
        Button queryData = (Button) findViewById(R.id.query_data);

        addData.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues values = new ContentValues();
                values.put("name", "测试");
                values.put("pd", 123);
                Log.d("TAG", "存入");
                db.insert("Test", null, values);
            }
        });
        queryData.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor cursor = db.query("Test", null, null, null, null, null, null);
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        int pages = cursor.getInt(cursor.getColumnIndex("pd"));
                        Log.d("TAG", "name is " + name);
                        Log.d("TAG", "password is " + pages);
                    }
                }
                cursor.close();
            }
        });
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

布局文件

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/add_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="添加数据" />

    <Button
        android:layout_below="@id/add_data"
        android:id="@+id/query_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查询数据" />

</RelativeLayout>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

下图是程序运行的截图。 
这里写图片描述

点击添加数据和查询数据,看打印日志 
这里写图片描述

第三步:将数据导出(需要root权限,我这里使用的是Genymotion模拟器,已root)。在根目录/data/data/应用包名中导出

这里写图片描述

在桌面使用SQLite Expert Personal 3工具打开导出的my.db数据库。 
提示出错。如下图: 
这里写图片描述

以上是对数据库文件的加密。下一篇介绍如何解密。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值