【原文翻译】Android应用程序集成SQLCipher实现SQLite加密

适用于Android应用程序集成的SQLCipher


社区版集成


适用于Android的SQLCipher社区版本通过AAR软件包格式分发,允许在基于Java或Kotlin的Android项目中使用。通过将以下条目添加到app / build.gradle文件的dependencies部分中,可以执行Gradle中的集成:

implementation 'net.zetetic:android-database-sqlcipher:4.4.2@aar'
implementation "androidx.sqlite:sqlite:2.0.1"

商业版集成


本教程将涵盖将SQLCipher for Android的二进制文件集成到现有的Android应用程序中。本教程假定开发人员具有适用于Android商业二进制文件的最新SQLCipher。

我们需要将AAR库文件复制到应用程序的app libs目录中。执行以下命令:

% cp android-database-sqlcipher-4.4.2.aar app/libs

接下来,修改项目的build.gradle文件,添加以下flatDir元素:

allProjects {
  repositories {
    jcenter()
    flatDir {
      dirs 'libs'
    }

  }
}

在应用程序模块build.gradle中,将以下内容添加到dependencies块中:

implementation (name: 'android-database-sqlcipher-4.4.2', ext: 'aar')
implementation "androidx.sqlite:sqlite:2.0.1"

仅对于商业版,通过SQLiteDatabaseHook打开连接时可能会提供许可证密钥。下面是一个示例:

SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {
  public void preKey(SQLiteDatabase database) {
    database.rawExec(String.format("PRAGMA cipher_license = '%s';", LICENSE_KEY));
  }
  public void postKey(SQLiteDatabase database) {}
};
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databasePath.getAbsolutePath(), password, null, hook);

应用用途


接下来,我们将修改活动的源,以正确初始化SQLCipher的本机库,然后创建插入记录的数据库文件。特别是,请注意net.sqlcipher.database.SQLiteDatabase的导入,而不是android.database.sqlite.SQLiteDatabase的导入,以及对SQLiteDatabase.loadLibs(this)的调用。对SQLiteDatabase.loadLibs(this)的调用必须在任何其他数据库操作之前进行。以下是Java中的用法示例:

package com.demo.sqlcipher;

import java.io.File;
import net.sqlcipher.database.SQLiteDatabase;
import android.app.Activity;
import android.os.Bundle;

public class HelloSQLCipherActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        InitializeSQLCipher();
    }

    private void InitializeSQLCipher() {
        SQLiteDatabase.loadLibs(this);
        File databaseFile = getDatabasePath("demo.db");
        databaseFile.mkdirs();
        databaseFile.delete();
        SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, "test123", null);
        database.execSQL("create table t1(a, b)");
        database.execSQL("insert into t1(a, b) values(?, ?)", new Object[]{"one for the money",
                                                                        "two for the show"});
    }
}

适用于Android的SQLCipher也可以与Kotlin无缝使用:

package com.demo.sqlcipher

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import net.sqlcipher.database.SQLiteDatabase

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        SQLiteDatabase.loadLibs(this)
        val databaseFile = getDatabasePath("demo.db")
        if(databaseFile.exists()) databaseFile.delete()
        databaseFile.mkdirs()
        databaseFile.delete()
        val database = SQLiteDatabase.openOrCreateDatabase(databaseFile, "test123", null)
        database.execSQL("create table t1(a, b)")
        database.execSQL("insert into t1(a, b) values(?, ?)",
            arrayOf<Any>("one for the money", "two for the show")
        )
    }
}

现在,该应用程序应该能够在模拟器或设备上运行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值