开发者

sqlite encryption for android

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?


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


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=..."


If anyone is still looking:

Override SQLiteOpenHelper function as below:

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜