Realm database decryption failed

老子叫甜甜 提交于 2019-12-04 06:14:37

问题


Question : When I try to open encrypted realm file in Realm Browser (windows) and it gives me following message.

Either this is not a Realm file or it's encrypted.

Enter: 128-character hex-encoded encryption key

Important - I am closing my realm before saving on disk.

Code

RealmConfiguration config = new RealmConfiguration.Builder()
            .name("w5uyqFyEDEK_OCWyl4123aa77")
            .schemaVersion(2)
            .encryptionKey(myClassObject.getRealmKey())
            .deleteRealmIfMigrationNeeded()
            .build();

Methods

 public byte[] getRealmKey() {
    byte[] key;
    String savedKey = getStringFromPrefs(KEY);
    if (savedKey.isEmpty()) {
        key = generateKey();
        String keyString = encodeToString(key);
        saveStringToPrefs(keyString);
    } else {
        key = decodeFromString(savedKey);
    }
    return key;
}



 private void saveStringToPrefs(String aKeyString) {
    pref.edit().putString(KEY, aKeyString).apply();
}

private String encodeToString(byte[] aKey) {
    AppLogger.d("Encoding Key: %s", Arrays.toString(aKey));
    return Base64.encodeToString(aKey, Base64.DEFAULT);
}

private byte[] decodeFromString(String aSavedKey) {
    byte[] decoded = Base64.decode(aSavedKey, Base64.DEFAULT);
    AppLogger.d("Decoded Key: ", Arrays.toString(decoded));
    return decoded;
}

private byte[] generateKey() {
    byte[] key = new byte[64];
    new SecureRandom().nextBytes(key);
    return key;
}

I have tried to open encrypted realm file using key stored in preference and byte[] decodedKey but still I am not able to open realm file.

Am I missing something here?


回答1:


You need to Hex encode your encryption key, not Base64 encode it.

You can see how to do it here: https://github.com/realm/realm-java/pull/5571



来源:https://stackoverflow.com/questions/47505171/realm-database-decryption-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!