简体   繁体   中英

How to apply encryption to an existing realm database in Android?

I have gone through this question and similar questions, but I am still unable to figure this out.

Earlier I was using SQLite for my android app. Size of pre-populated Sqlite database is about 15 MB. I made an temporary android app to my copy my records into a new Realm database. The size of this new Realm database was about 150 MB. Then I opened it with Realm browser in Mac and compressed it. Size now reduced back to 15 MB.

But if I use encryption while creating new database and copying sqlite rows into it, size is approx 150 MB. When I opened it using Hex password, it opens fine. Then I compressed it as earlier -> size back to normal 15 MB. But, I don't know why, the encryption is removed now .

To counter this, I can only think of one solution. If I could apply encryption to already compressed Realm database. But, I couldn't figure out, how to do this in Android?

Edit - Some relevant parts -

Part 1 -

Realm.init(this);
byte[] key = new byte[64]; // Just for demonstration
RealmConfiguration config = new RealmConfiguration.Builder()
                .name("QBank.realm")
                .directory(getExternalFilesDir(null))
                .encryptionKey(key)
                .build();
Realm realm = Realm.getInstance(config);

Part 2 -

 @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_sqlite2realm:
                DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
                databaseAccess.open();
                questionsList = databaseAccess.getAllQuestions();
                databaseAccess.close();

                Realm realm = Realm.getInstance(config);
                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        for (QuestionBank question : questionsList){
                            realm.copyToRealmOrUpdate(question);
                        }
                    }
                });
                break;
        }
    }

For converting Encrypted database to Compressed database -> I pulled this realm database from android emulator -> opened it in Realm Browser (with Hex password) -> Compressed (but this newly created compressed database is now not encrypted)

PS - I know my question is descriptive, but I couldn't find a way to do this.

I think this question pointing to same problem as you are facing.

Compression requires finding patterns in the data. Encryption removes all such patterns.

You need to do the compression before the encryption. Encryption turns your data into high-entropy data, usually indistinguishable from a random stream. Compression relies on patterns in order to gain any size reduction. Since encryption destroys such patterns, the compression algorithm is unable to give you much reduction in size if you apply it to encrypted data.

Let me know for any other help.

Hope this will help you. Happy Coding!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM