从未加密的域迁移到加密的域Android

时间:2016-07-05 11:24:11

标签: android realm

我们有一个使用领域的应用程序版本,但它没有加密并且已经发布。但现在我们想用最新版本加密realm文件。所以在Application类的onCreate()中,我调用了下面的方法。

fun setRealmMigrationConfiguration() {
    realmConfiguration = RealmConfiguration.Builder(this)
            .schemaVersion(1)
            .migration(RealmMigrationClass())
            .build()
    Realm.setDefaultConfiguration(realmConfiguration)

    keyStore.load(null)
    val realmKey = "realmKey"


    val realmKeyFile = File("${this.filesDir}/$realmKey")

    if(keyStore.containsAlias(alias)) {
        if(!realmKeyFile.exists()) {
            keyStore.deleteEntry(alias)
        }
    }

    val encryptedRealmFileName = "default.realm.enc"

    if (!keyStore.containsAlias(alias)) {

        //Generates key pair in key store
        generateKeyPairInKeyStore(alias)

        val privateKeyEntry = keyStore.getEntry(alias, null) as KeyStore.PrivateKeyEntry
        val publicKey = privateKeyEntry.certificate.publicKey

        val key = generateKey()

        val encryptedKey = encryptKey(publicKey,key)

        realmKeyFile.writeBytes(encryptedKey)

        val unencryptedRealm = File("${this.filesDir}/default.realm")
        val encryptedRealm = File("${this.filesDir}/$encryptedRealmFileName")


        if (unencryptedRealm.exists() && !encryptedRealm.exists()) {
            val realm = Realm.getDefaultInstance()
            realm.writeEncryptedCopyTo(encryptedRealm, key)
            realm.close()

            Realm.removeDefaultConfiguration()
        }

        realmConfiguration = RealmConfiguration.Builder(this)
                                    .schemaVersion(1)
                                    .migration(RealmMigrationClass())
                                    .name(encryptedRealmFileName)
                                    .encryptionKey(key)
                                    .build()

        Realm.setDefaultConfiguration(realmConfiguration)

    } else {
        val privateKeyEntry = keyStore.getEntry(alias, null) as KeyStore.PrivateKeyEntry
        val privateKey = privateKeyEntry.privateKey

        val keyFile = realmKeyFile.readBytes()
        val buffer = decryptKey(privateKey,keyFile)

        realmConfiguration = RealmConfiguration.Builder(this)
                .schemaVersion(1)
                .migration(RealmMigrationClass())
                .name(encryptedRealmFileName)
                .encryptionKey(buffer)
                .build()

        Realm.removeDefaultConfiguration()
        Realm.setDefaultConfiguration(realmConfiguration)

        info { "File =>${realmConfiguration?.path}" }
    }
}

但是当应用程序从旧版本更新到新版本时,我会遇到异常。它无法解密已加密的文件。我确保密钥正确,并且RealmConfiguration中给出的文件名是正确的。

java.lang.IllegalArgumentException: Illegal Argument: Invalid format of Realm file.
at io.realm.internal.SharedGroup.createNativeWithImplicitTransactions(Native Method)
at io.realm.internal.SharedGroup.<init>(SharedGroup.java:64)
at io.realm.internal.s.<init>(SharedGroupManager.java:49)
at io.realm.a.<init>(BaseRealm.java:79)
at io.realm.r.<init>(Realm.java:138)
at io.realm.r.b(Realm.java:248)
at io.realm.r.a(Realm.java:228)
at io.realm.u.a(RealmCache.java:126)
at io.realm.r.m(Realm.java:174)

我不知道我在这里做错了什么。有人能指出这里出了什么问题吗?

我打印了用于加密领域文件的密钥(字节数组)并解密了领域文件。它完全一样

0 个答案:

没有答案