sqlcipher 加密数据库的异常

前言

通常我们在做移动应用开发的时候,数据存储是我们一直关注的问题,最常用的当属于数据库,由于考虑到安全性,我们通常采用加密过的数据库,sqlciper便是我们最常见的选择.本文主要描述使用过程中遇到的问题集合.

引入版本

  implementation 'net.zetetic:android-database-sqlcipher:4.2.0@aar'

发现问题

java.lang.RuntimeException: Unable to get provider com.closeli.industry.business.database.CacheDataProvider: 
net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
....
Caused by: net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
....
  • 通过异常信息,我们看到的为之前的数据库文件不被识别,导致加载出现了异常,又因为该库为第三方工具,我们便需要去查看部分源码,或者去对应的仓库地址查看该问题.

解决方法

  • 查看 加密数据的源码,发现有一个这样的重载方法.
/**
     * Create a helper object to create, open, and/or manage a database.
     * The database is not actually created or opened until one of
     * {@link #getWritableDatabase} or {@link #getReadableDatabase} is called.
     *
     * @param context to use to open or create the database
     * @param name of the database file, or null for an in-memory database
     * @param factory to use for creating cursor objects, or null for the default
     * @param version number of the database (starting at 1); if the database is older,
     *     {@link #onUpgrade} will be used to upgrade the database
     * @param hook to run on pre/post key events
     */
    public SQLiteOpenHelper(Context context, String name, CursorFactory factory,
                            int version, SQLiteDatabaseHook hook) {
        this(context, name, factory, version, hook, new DefaultDatabaseErrorHandler());
    }
  • 具体方式
new SQLiteDatabaseHook() {
            @Override
            public void preKey(SQLiteDatabase database) {

            }

            @Override
            public void postKey(SQLiteDatabase sqLiteDatabase) {
                sqLiteDatabase.execSQL("PRAGMA cipher_page_size = 1024");
                sqLiteDatabase.execSQL("PRAGMA kdf_iter = 64000");
                sqLiteDatabase.execSQL("PRAGMA cipher_hmac_algorithm = HMAC_SHA1");
                sqLiteDatabase.execSQL("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1");
            }
        }

结束语

当我们在使用第三方工具时,遇到问题我们应当首先去对应的仓库中去查看该问题是否存在,其次去查看源码,定位问题的所在.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLCipher 是一个基于SQLite加密扩展,可以用于在C#应用程序中为SQLite数据库提供加密功能。在C#中使用SQLCipher加密SQLite数据库需要以下步骤: 1. 下载SQLCipher库文件并将其添加到C#项目中。 2. 在C#代码中使用SQLiteConnection类创建一个SQLite数据库连接,并使用SQLCipher加密密钥对其进行加密。 ``` using System.Data.SQLite; using System.Security.Cryptography; string databasePath = @"C:\path\to\database.db"; string password = "myPassword"; byte[] salt = new byte[16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 }; byte[] key = new Rfc2898DeriveBytes(password, salt).GetBytes(16); SQLiteConnection connection = new SQLiteConnection("Data Source=" + databasePath); connection.SetPassword(key); connection.Open(); ``` 在上面的示例中,我们使用Rfc2898DeriveBytes类从密码和盐中生成16字节的加密密钥,并使用SetPassword方法将其应用于SQLite连接。 3. 执行SQL命令来创建表和插入数据等操作。 ``` SQLiteCommand command = new SQLiteCommand("CREATE TABLE myTable (id INTEGER PRIMARY KEY, name TEXT)", connection); command.ExecuteNonQuery(); command = new SQLiteCommand("INSERT INTO myTable (name) VALUES ('John Doe')", connection); command.ExecuteNonQuery(); ``` 在上面的示例中,我们使用SQLiteCommand类来执行SQL命令。 4. 在完成对数据库的操作后,关闭SQLite连接。 ``` connection.Close(); ``` 以上就是在C#应用程序中使用SQLCipher加密SQLite数据库的步骤。需要注意的是,为了保证安全性,应该使用强密码来生成加密密钥,并且不要将密码硬编码到应用程序中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值