when i'm trying to use encrypt feature on my project, i get this error:
this is my Application class content:
/* SetUp Application database as encrypted contents */
byte[] key = new byte[64];
new SecureRandom().nextBytes(key);
Realm.init(this);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.encryptionKey(key)
.name("Application.db")
.deleteRealmIfMigrationNeeded()
.build();
full stack error:
Caused by: io.realm.exceptions.RealmFileException: Unable to open a realm at path '/data/data/com.pishguy.androidapplication.cafemaku/files/Application.db': Realm file decryption failed. in /Users/emanuelez/repos/realm/realm-java-private/realm/realm-library/src/main/cpp/io_realm_internal_SharedRealm.cpp line 81 Kind: ACCESS_ERROR.
Your key needs to be the same when you open the Realm.
Which means you currently just randomize a key and then forget it, which means you essentially "won't know the password" to unlock your "account" (read: Realm)
It's like mashing random buttons on your keyboard when you first create a password, and then you won't be able to login again.
You're supposed to remember the key you use to Encrypt the Realm
@Zhuinden it means i must to use object server?
.... What? No, you just shouldn't do new SecureRandom().nextBytes(key); because you create a new key each time, and you must use the SAME key to decrypt as you used to encrypt
@Zhuinden oh! yes you right, Thanks how can i use new SecureRandom().nextBytes(key); and decrypt after encrypt on each using application sir? is any documentation about that?
... I've heard something about a "KeyStore" that you're supposed to store things in on Android, but I don't know much about it nor its reliability
@Zhuinden Thanks sir, i'm trying to search more about that
The Realm documentation for Encryption is not very clear: https://realm.io/docs/java/latest/. The code to the example in GitHub is incomplete. They should change it.
@brayanL can you please let us know which part about the encryption is not very clear and incorrect?
@beeender i think he means there is no best practice on where to store the encryption key
@beeender The code presented in the documentation only shows a simple example of how to generate an encryption key, but does not show how to store correctly and use it later.
@Zhuinden @brayanL we have a PR about storing key https://github.com/realm/realm-java/pull/4181 , but never get enough time to finish it...
is there a way to catch the error, Realm file decryption failed. There might be a great chance when the decryption key is not available and the Realm throws this error. How to catch this error and delete the existing Realm and create a new one with the new Encryption Key. For testing, I have stored the Realm Key in the Shared Preferences and getting the encryption key in the Application Class before i initialize the Realm Object. But sometimes, the key i am getting is different or null or empty and the Realm throws the error. How to handle this situation?
We cannot tell the difference between the wrong encryption key and a corrupt/non-Realm file being accessed. So all you can do is catching the RealmFileException and delete the Realm file.
try {
Log.d("upisdk", "Realm Initiated...");
Realm.init(App.this);
RealmConfiguration config = new RealmConfiguration.Builder()
.encryptionKey(REALM_KEY.getBytes())
.schemaVersion(0)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
Realm.getInstance(config);
} catch (RealmFileException e) {
Log.d("upisdk", "Realm file expception");
Realm.deleteRealm(Realm.getDefaultInstance().getConfiguration());
}
This is how I am initializing and deleting the Realm Database and probably initiate the new Realm Db there only. But whenever i am getting the exception, i am still getting the error on this line, Realm.deleteRealm(Realm.getDefaultInstance().getConfiguration()); How to delete the Realm Db?
Help would be appreciated. I have tagged u on Stackoverflow also.
You cannot delete a Realm while it is open and Realm.getDefaultInstance will open a Realm.
Do this instead:
Realm.deleteRealm(Realm.getDefaultConfiguration());
Thank you, sir. You are awesome.
Most helpful comment
... I've heard something about a "KeyStore" that you're supposed to store things in on Android, but I don't know much about it nor its reliability