Uploaded image for project: 'Realm Kotlin'
  1. Realm Kotlin
  2. RKOTLIN-1047

[RLM_ERR_INVALID_DATABASE] Realm cannot be decrypted after a migration from java-sdk to kotlin-sdk

      How frequently does the bug occur?

      Always

      Description

      We've recently decided to migrate from java-sdk to to the kotlin-sdk and when the app is first started right after the migration, the encrypted realm file cannot be opened with the same key that has been used to encrypt the realm prior to the migration to the kotlin-sdk. The error we're getting is as follows:

      [RLM_ERR_INVALID_DATABASE]: Failed to open Realm file at path '/data/user/0/com.example.app/files/default.realm': Realm file decryption failed (Decryption failed: 'unable to decrypt after 0 seconds (retry_count=4, from=i != bytes_read, size=4096)')

      Here are details about the SDK versions:

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      // Java-sdk version prior to migration
      implementation("io.realm:realm-gradle-plugin:10.11.1")
      
      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      // Kotlin-sdk version after the migration
      implementation ("io.realm.kotlin:library-base:1.11.0")
      

      We've verified that the key used for the realm config is the same before and after the migration so there should be no issue with the key itself. In addition, we did tests with unencrypted realm and in this case we don't have issues with the migration.

      In addition, we tried to open the realm file using Realm Studio by providing the encryption key. We get warning that the realm file is in an outdated format which I'm not sure is related to the issue but after choosing the Backup and upgrade option, we are able to decrypt the realm database and see the content - everything is in there.

      Here is the code for generating the realm config:

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      private fun initRealmConfig(): RealmConfiguration? {
              // Increment whenever you made changes to the Realm* database models
              val schemaVersion: Long = 2
              
              // Get encryption key
              val realmKey = DBHelper.loadEncryptionKey(App.applicationContext())
      
              realmKey?.let {
                  val realmConfig = RealmConfiguration.Builder(getSchemaClasses())
                      .schemaVersion(schemaVersion)
                      .migration(DBMigrationHelper.migrate())
                      .encryptionKey(it)
                      .build()
      
                  // Once we've used the key to generate a config, erase it in memory manually
                  Arrays.fill(realmKey, 0.toByte())
      
                  return realmConfig
              }
      
              return null
          }
      

      Stacktrace & log output

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      [RLM_ERR_INVALID_DATABASE]: Failed to open Realm file at path '/data/user/0/com.example.app/files/default.realm': Realm file decryption failed (Decryption failed: 'unable to decrypt after 0 seconds (retry_count=4, from=i != bytes_read, size=4096)')
      

      Can you reproduce the bug?

      Always

      Reproduction Steps

      1. Run app version with java-sdk
      2. Migrate to kotlin-sdk
      3. Update the installed app version in step 1 with the migrated version

      Problem

      • Realm decryption fails

      Version

      1.11.0

      What Atlas App Services are you using?

      Local Database only

      Are you using encryption?

      Yes

      Platform OS and version(s)

      Android

      Build environment

      Android Studio version: Hedgehog | 2023.1.1 Patch 2
      Android Build Tools version: com.android.tools.build:gradle:7.2.2
      Gradle version: gradle-7.4-bin

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: