Changing Database Encryption Key

To change database encryption key, create the RekeyBase application that uses the framework methods:

Method name Brief description
openBase(path: String, key: String) The method opens or creates a database.
reKeyBase(path: String, new_key: String) The method changes encryption key of opened database.

The application includes a screen, a text box for entering encryption key, and buttons:

To execute the example:

  1. Enter the primary database encryption key to the text box and click the Open Database button.

Clicking the button opens a connection or creates a database with the specified primary key. The text view describes the method execution result.

  1. Enter the new database encryption key to the text box and click Rekey Database button.

Clicking the button changes database encryption key. The text view describes the method execution result.

  1. Enter the primary database encryption key to the text box and click the Open Database button.

Clicking the button closes the previous database connection and attempts to open a new connection with the primary key. The text view describes the method execution result.

  1. Enter the new database encryption key specified in Step 2 and click the Open Database button.

Clicking the button closes the previous database connection and attempts to open a new connection with the new key. The text view describes the method execution result.

Application code:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import com.mobrun.plugin.api.HyperHive;
public class ReKeyActivityJava extends Activity
{
  private static final String TAG = ReKeyActivityJava.class.getSimpleName();
  private HyperHive hyperHive;
  @Override
  protected void onCreate(final Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    hyperHive = Util.INSTANCE.getHyperHive(this);
    initView();
  }
  /**
   * Initialize views
   */
  private void initView()
  {
    setContentView(R.layout.activity_rekey);
    final EditText key = (EditText) findViewById(R.id.key); // box with key
    final TextView result = (TextView) findViewById(R.id.result); // text with result
    final String db_path = Util.INSTANCE.getDbPath(this) + "_rekeytest.sqlite"; // path to database
    // Open database
    findViewById(R.id.btnOpen).setOnClickListener(v ->
    {
      final String response = "open  : " + hyperHive.databaseAPI.openBase(db_path, key.getText().toString());
      Log.d(TAG, "onClick() : open  : " + db_path + " : " + response + " : " + key.getText());
      result.setText(response);
    });
    // Set new encryption key
    findViewById(R.id.btnReKey).setOnClickListener(v ->
    {
      final String response = "rekey : " + hyperHive.databaseAPI.reKeyBase(db_path, key.getText().toString());
      Log.d(TAG, "onClick() : rekey : " + db_path + " : " + response + " : " + key.getText());
      result.setText(response);
      hyperHive.databaseAPI.closeBase(db_path);
    });
  }
}
import android.app.Activity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_rekey.*
import kotlinx.android.synthetic.main.activity_rekey.view.*
class ReKeyActivity : Activity()
{
  private lateinit var hyperHive: HyperHive
  override fun onCreate(savedInstanceState: Bundle?)
  {
    super.onCreate(savedInstanceState)
    hyperHive = Util.getHyperHive(this)
    initView()
  }
  /**
   * Initialize views
   */
  private fun initView()
  {
    setContentView(R.layout.activity_rekey)
    val db_path = Util.getDbPath(this) // path to database
    // Open database
    root.btnOpen.setOnClickListener(
    {
      val response = "open  : ${hyperHive.databaseAPI.openBase(db_path, root.key.text.toString())}"
      Log.d(TAG, "onClick() : open  : $db_path : $response : ${root.key.text}")
      root.result.text = response
    })
    // Set new encryption key
    root.btnReKey.setOnClickListener(
    {
      val response = "rekey : ${hyperHive.databaseAPI.reKeyBase(db_path, root.key.text.toString())}"
      Log.d(TAG, "onClick() : rekey : $db_path : $response : ${root.key.text}")
      root.result.text = response
      hyperHive.databaseAPI.closeBase(db_path)
    })
  }
  companion object
  {
    private val TAG = ReKeyActivity::class.java.simpleName
  }
}

See also:

Examples of Android Framework Use | Examples of Working with Resources