title: Android Realm Encryption Example
body: <p>I have created Realm Encryption Example project source from following link <a href="https://github.com/realm/realm-java/tree/master/examples/encryptionExample/src/main/java/io/realm/examples/encryptionexample" rel="nofollow">https://github.com/realm/realm-java/tree/master/examples/encryptionExample/src/main/java/io/realm/examples/encryptionexample</a> .When i run the project without any change in the code, it runs without error.but i comment the following line in source</p>
  
  <pre><code>Realm.deleteRealm(realmConfiguration);
  </code></pre>
  
  <p>because no need to delete old file for me.
  then i start the application.it throws error java.lang.IllegalArgumentException: Illegal Argument :Invalid Format of Realm File.</p>
  
  <p>How can i read Realm file with same encryption key.</p>
  
  <p>Source Code:</p>
  
  <pre><code>    package io.realm.examples.encryptionexample;
  
  import android.app.Activity;
  import android.os.Bundle;
  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);
          RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
                  .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.beginTransaction();
          Person person = realm.createObject(Person.class);
          person.setName("Happy Person");
          person.setAge(14);
          realm.commitTransaction();
  
          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.
      }
  }
  </code></pre>
  
  <p>Thanks in Advance.</p>