KMI
KMI

Reputation: 258

how to use Encrypting Realm with key stored in KeyStore in API level 18

How I can create Keystore and set/get my realm key. in target api 24 and minapi 18

Realm.init(this);
    RealmConfiguration realmConfig = new RealmConfiguration.Builder()
            .encryptionKey(getKey())
            .name("realm")
            .schemaVersion(0)
            .deleteRealmIfMigrationNeeded()
            .build();
    Realm.setDefaultConfiguration(realmConfig);

public byte[] getKey() {
    if key exist return else generate new SecureRandom().nextBytes(key)
}

Upvotes: 3

Views: 744

Answers (1)

user5178068
user5178068

Reputation:

You can generate RSA keys in the keystore since your min sdk is set to 18. https://developer.android.com/training/articles/keystore.html

You then can use those to encrypt a generated AES key and store that in the shared preferences to use as your realm encryption key.

Upvotes: 0

Related Questions