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:


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) { = name;

    public int getAge() {
        return age;

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


package io.realm.examples.encryption;

public class Util {

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

    //Original source:
    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);



package io.realm.examples.encryption;


import io.realm.Realm;

public class MyApplication extends Application {

    public void onCreate() {


package io.realm.examples.encryption;

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


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

public class EncryptionExampleActivity extends Activity {

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

    private Realm realm;

    protected void onCreate(Bundle 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:
        // *
        // *
        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:
        // The path is normally /data/data/io.realm.examples.encryption/files/default.realm
        Log.i("RealmEncryptionKey", Util.bytesToHex(key));

        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()

        // Start with a clean slate every time

        // 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() {
            public void execute(Realm realm) {
                Person person = realm.createObject(Person.class);
                person.setName("Happy Person");

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

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


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

Read More.

Related Posts