Update / Change Realm Encryption Key

I encrypted the Realm database in my application. I would like to change the encryption key. Is it right to do this by creating a copy of the Realm file with a new encryption key, or is there another option available?

Thanks.

+5
source share
1 answer

Yes, you must make a copy of the Realm file with the new encryption key. This method is called writeEncryptedCopyTo() : https://realm.io/docs/java/latest/api/io/realm/Realm.html#writeEncryptedCopyTo-java.io.File-byte:A-

Something like 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() 
+3
source

Source: https://habr.com/ru/post/1237731/


All Articles