1

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)

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

Community
  • 1
  • 1
Dr. Atul Tiwari
  • 1,085
  • 5
  • 22
  • 46
  • Are you closing your realm db? If possible share your code for copy? – Pravin Divraniya Dec 05 '16 at 14:58
  • Sorry I didn't get you. You mean code for copying from `sqlite` to `realm` or from encrypted to compressed? – Dr. Atul Tiwari Dec 05 '16 at 15:16
  • @PravinD, I have updated my question, with relevant information. – Dr. Atul Tiwari Dec 05 '16 at 15:24
  • Have you tried compression first and then encryption? – Pravin Divraniya Dec 06 '16 at 07:11
  • You can write a copy of your Realm using `writeEncryptedCopyTo`. The method will encrypt using a new key and compact the Realm. Take a look at https://realm.io/docs/java/2.2.1/api/io/realm/Realm.html#writeEncryptedCopyTo-java.io.File-byte:A- – geisshirt Dec 06 '16 at 09:09
  • @PravinD that's what I wanted to try (compress first, then encryption), but I didn't know how to encrypt the existing realm database. I think, what @geisshirt has suggested, `writeEncryptedCopyTo`, should do the desired thing. Will update you both once I get to try this. – Dr. Atul Tiwari Dec 06 '16 at 10:52

1 Answers1

0

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!!

Community
  • 1
  • 1
Pravin Divraniya
  • 4,223
  • 2
  • 32
  • 49