Code Example

Kotlin Android Realm Encryption Snippets

A step by step Android Realm Encryption example.

1. Realm Encryption

Awaiting below is a full android sample to demonstrate encrypttion in Realm.

Step 1. Write Code

Finally we need to write our code as follows:

(a). Person.java


package io.realm.examples.encryption;

import io.realm.RealmObject;

public class Person extends RealmObject {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

(b). Util.java


package io.realm.examples.encryption;

public class Util {

    private final static char[] hexArray = "0123456789ABCDEF".toCharArray();

    //Original source: https://stackoverflow.com/a/9855338/1389357
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for ( int j = 0; j < bytes.length; j++ ) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

}

(c). MyApplication.java


package io.realm.examples.encryption;

import android.app.Application;

import io.realm.Realm;

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Realm.init(this);
    }
}

(d). EncryptionExampleActivity.java


package io.realm.examples.encryption;

import android.app.Activity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

import java.security.SecureRandom;

import io.realm.Realm;
import io.realm.RealmConfiguration;

public class EncryptionExampleActivity extends Activity {

    public static final String TAG = EncryptionExampleActivity.class.getName();

    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Generate a key
        // IMPORTANT! This is a silly way to generate a key. It is also never stored.
        // For proper key handling please consult:
        // * https://developer.android.com/training/articles/keystore.html
        // * http://nelenkov.blogspot.dk/2012/05/storing-application-secrets-in-androids.html
        byte[] key = new byte[64];
        new SecureRandom().nextBytes(key);

        // An encrypted Realm file can be opened in Realm Studio by using a Hex encoded version
        // of the key. Copy the key from Logcat, then download the Realm file from the device using
        // the method described here: https://stackoverflow.com/a/28486297/1389357
        // The path is normally /data/data/io.realm.examples.encryption/files/default.realm
        Log.i("RealmEncryptionKey", Util.bytesToHex(key));

        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
                .encryptionKey(key)
                .build();

        // Start with a clean slate every time
        Realm.deleteRealm(realmConfiguration);

        // Open the Realm with encryption enabled
        realm = Realm.getInstance(realmConfiguration);

        // Everything continues to work as normal except for that the file is encrypted on disk
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                Person person = realm.createObject(Person.class);
                person.setName("Happy Person");
                person.setAge(14);
            }
        });

        Person person = realm.where(Person.class).findFirst();
        Log.i(TAG, String.format("Person name: %s", person.getName()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.close(); // Remember to close Realm when done.
    }
}

Reference

You can DOWNLOAD FULL CODE.
You can also browse code or read more here.
Follow code author here.


Read More.

Related Posts