I have encrypted Realm
database in my application. I would like to change the encryption key. Is the proper way to do this making a copy of the Realm
file with new encryption key or is there some another option available?
Thanks.
Yes, you have to make a copy of the Realm file with the new encryption key. The method is called writeEncryptedCopyTo()
: https://realm.io/docs/java/latest/api/io/realm/Realm.html#writeEncryptedCopyTo-java.io.File-byte:A-
Something like the below should work:
RealmConfiguration config1 = new RealmConfiguration.Builder(context)
.name("old-name")
.encryptionKey(getOldKey())
.build()
Realm realm = Realm.getInstance(config1);
realm.writeEncryptedCopyTo(new File(context.getFilesDir(), "new-name"), getNewKey());
realm.close();
RealmConfiguration config2 = new RealmConfiguration.Builder(context)
.name("new-name")
.encryptionKey(getNewKey())
.build()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With