mirror of https://github.com/beemdevelopment/Aegis
Add an activity to decrypt the database
parent
53e86db187
commit
722ea50b68
@ -0,0 +1,79 @@
|
||||
package me.impy.aegis;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKey;
|
||||
|
||||
import me.impy.aegis.crypto.CryptoUtils;
|
||||
import me.impy.aegis.crypto.MasterKey;
|
||||
import me.impy.aegis.crypto.slots.PasswordSlot;
|
||||
import me.impy.aegis.crypto.slots.Slot;
|
||||
import me.impy.aegis.crypto.slots.SlotCollection;
|
||||
|
||||
public class AuthActivity extends AppCompatActivity {
|
||||
public static final int RESULT_OK = 0;
|
||||
public static final int RESULT_EXCEPTION = 1;
|
||||
|
||||
private EditText textPassword;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_auth);
|
||||
textPassword = (EditText) findViewById(R.id.text_password);
|
||||
|
||||
Intent intent = getIntent();
|
||||
final SlotCollection slots = (SlotCollection) intent.getSerializableExtra("slots");
|
||||
|
||||
Button button = (Button) findViewById(R.id.button_decrypt);
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
MasterKey masterKey = null;
|
||||
try {
|
||||
if (slots.has(PasswordSlot.class)) {
|
||||
PasswordSlot slot = slots.find(PasswordSlot.class);
|
||||
char[] password = getPassword(true);
|
||||
SecretKey key = slot.deriveKey(password);
|
||||
CryptoUtils.zero(password);
|
||||
Cipher cipher = Slot.createCipher(key, Cipher.DECRYPT_MODE);
|
||||
masterKey = MasterKey.decryptSlot(slot, cipher);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO: feedback
|
||||
throw new UndeclaredThrowableException(e);
|
||||
}
|
||||
|
||||
// send the master key back to the main activity
|
||||
Intent result = new Intent();
|
||||
result.putExtra("key", masterKey);
|
||||
setResult(RESULT_OK, result);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private char[] getPassword(boolean clear) {
|
||||
char[] password = getEditTextChars(textPassword);
|
||||
if (clear) {
|
||||
textPassword.getText().clear();
|
||||
}
|
||||
return password;
|
||||
}
|
||||
|
||||
private static char[] getEditTextChars(EditText text) {
|
||||
Editable editable = text.getText();
|
||||
char[] chars = new char[editable.length()];
|
||||
editable.getChars(0, editable.length(), chars, 0);
|
||||
return chars;
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fitsSystemWindows="true"
|
||||
tools:context="me.impy.aegis.AuthActivity">
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="344dp"
|
||||
android:layout_height="495dp"
|
||||
android:layout_margin="32dp"
|
||||
tools:layout_editor_absoluteY="8dp"
|
||||
tools:layout_editor_absoluteX="8dp">
|
||||
<TextView
|
||||
android:text="@string/authentication"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="24sp"
|
||||
android:textColor="@color/primary_text_inverted"
|
||||
android:id="@+id/textView2" />
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="12dp">
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/textView4"
|
||||
android:text="@string/authentication_enter_password"/>
|
||||
<EditText
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="textPassword"
|
||||
android:id="@+id/text_password"/>
|
||||
<Button
|
||||
android:id="@+id/button_decrypt"
|
||||
android:layout_width="125dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Decrypt" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</android.support.design.widget.CoordinatorLayout>
|
Loading…
Reference in New Issue