Android 的 sqlite 加密

发布于 2024-11-08 00:43:19 字数 735 浏览 0 评论 0原文

我正在努力寻找在 Android 设备上加密我的 sqlite 数据库的可能性,但我无法找到令人满意的解决方案。

我需要类似库之类的东西来引用,以便在使用正常的 sqlite 函数时对我的数据库进行“动态”加密/解密。

我不想在存储之前加密数据。

我不想加密整个数据库文件,以便在使用之前对其进行解密。

我知道以下项目:

但我找不到这个东西的任何工作示例。

顺便说一句,我绝对愿意购买商业版本,但在花费几百美元之前我必须先进行测试。

有人自己解决这个问题吗?

i'm looking very hard for a possibility to encrypt my sqlite database on Android devices, but I was't able to find a satisfying solution.

I need something like a libary to reference, in order to have a "on the fly" encryption/decryption of my database, while using the normal sqlite functions.

I don't want to encrypt data before storing.

I don't want to encrypt the whole databasefile, in order to decrypt it before using.

I know about the following projects:

But I can't find any working example for this stuff.

Btw, I'm absolutly willing to purchase a commercial build, but I have to test ist before spending a few hundred dollars.

Did anyone solve this issue for his own?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅笑轻吟梦一曲 2024-11-15 00:43:19

尝试SQLCipher 移植到 Android,而不是常规 SQLCipher。

Try the SQLCipher port to Android instead of the regular SQLCipher.

缪败 2024-11-15 00:43:19

litereplica 支持 使用 ChaCha 密码进行加密,在便携式设备上比 AES 更快。

Android 有绑定

要创建并打开加密数据库,我们使用如下 URI:

"file:/path/to/file.db?cipher=...&key=..."

litereplica supports encryption using the ChaCha cipher, faster than AES on portable devices.

There are bindings for Android.

To create and open an encrypted database we use an URI like this:

"file:/path/to/file.db?cipher=...&key=..."
紫﹏色ふ单纯 2024-11-15 00:43:19

如果有人仍在寻找:

重写 SQLiteOpenHelper 函数,如下所示:

void onConfigure(SQLiteDatabase db){
    db.execSQL("PRAGMA key = 'secretkey'");
}

If anyone is still looking:

Override SQLiteOpenHelper function as below:

void onConfigure(SQLiteDatabase db){
    db.execSQL("PRAGMA key = 'secretkey'");
}
各自安好 2024-11-15 00:43:19
private String encrypt(String password) {
   try {
        SecretKeySpec keySpec = generateKey(password);
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.ENCRYPT_MODE,keySpec);
        byte[] encVal = c.doFinal(password.getBytes());
        String encryptedValue = Base64.encodeToString(encVal,Base64.DEFAULT);
        return encryptedValue;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

private SecretKeySpec generateKey(String password) throws Exception {
    final MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
    digest.update(bytes,0,bytes.length);
    byte[] key = digest.digest();
    SecretKeySpec secretKeySpec = new SecretKeySpec(key,"AES");
    return secretKeySpec;
}

我只是使用加密函数来加密密码。这里我使用用户的密码作为密钥。因此我不需要将密钥保留在应用程序内。当用户想要登录时,只需对密码进行加密并尝试与数据库中的加密密码进行匹配并允许其登录。

private String encrypt(String password) {
   try {
        SecretKeySpec keySpec = generateKey(password);
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.ENCRYPT_MODE,keySpec);
        byte[] encVal = c.doFinal(password.getBytes());
        String encryptedValue = Base64.encodeToString(encVal,Base64.DEFAULT);
        return encryptedValue;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

private SecretKeySpec generateKey(String password) throws Exception {
    final MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
    digest.update(bytes,0,bytes.length);
    byte[] key = digest.digest();
    SecretKeySpec secretKeySpec = new SecretKeySpec(key,"AES");
    return secretKeySpec;
}

I just used the encrypt function to encrypt the password. Here I used the user's password as a key. Therefore I don't need to keep the key inside the application. When the user wants to log in, simply encrypt the password and try to match with the encrypted password in the database and allow them to log in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文