Almost done change password

This commit is contained in:
Wiktor Bałaban 2019-11-19 18:47:28 +01:00
parent 720d5e54c1
commit 25bc253d58
6 changed files with 196 additions and 2 deletions

View File

@ -10,10 +10,14 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ChangePassword"
android:label="@string/title_activity_change_password"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity
android:name=".NotebookActivity"
android:label="@string/title_activity_notebook"
android:theme="@style/AppTheme.NoActionBar"></activity>
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".SetPasswordActivity"
android:label="@string/title_activity_set_password"

View File

@ -0,0 +1,95 @@
package com.example.encryptednotebook;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.EditText;
import javax.crypto.SecretKey;
import static com.example.encryptednotebook.Cipher.decryptMsg;
import static com.example.encryptednotebook.Cipher.encryptMsg;
import static com.example.encryptednotebook.Cipher.generateKey;
public class ChangePassword extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_password);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText setPass1 = findViewById(R.id.setPass1);
String setPass1Value = setPass1.getText().toString();
EditText setPass2 = findViewById(R.id.setPass2);
String setPass2Value = setPass2.getText().toString();
if (setPass1Value.equals(setPass2Value)) {
try {
String encryptedText = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("TEXT", null);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
try {
EditText password = findViewById(R.id.oldPass);
String passValue = password.getText().toString();
String savedPassValue = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("PASSWORD", null);
SecretKey oldSecretKey = generateKey(passValue, getApplicationContext());
String savedPassValueDecrypted = decryptMsg(savedPassValue, oldSecretKey);
if (passValue.equals(savedPassValueDecrypted)) {
Snackbar.make(view, "Dobre hasło", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
SecretKey newSecretKey = generateKey(setPass2Value, getApplicationContext());
String encryptedText = encryptMsg(setPass2Value, newSecretKey);
prefs.edit().putBoolean("PASSWORD_CREATED", true).apply();
prefs.edit().putString("PASSWORD", encryptedText).apply();
String encryptedOldText = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("TEXT", null);
if(encryptedOldText!=null){
String decrptedText = decryptMsg(encryptedOldText, oldSecretKey);
String encrptedNewText = encryptMsg(decrptedText, newSecretKey);
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("TEXT", encrptedNewText).apply();
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
} else {
Snackbar.make(view, "Złe hasło", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}catch(Exception e){
e.printStackTrace();
Snackbar.make(view, "Złe hasło", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}catch (Exception e){
e.printStackTrace();
}
} else {
Snackbar.make(view, "Hasła się różnią, wpisz takie same hasła", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
});
}
}

View File

@ -17,6 +17,7 @@ import android.widget.EditText;
import javax.crypto.SecretKey;
import static com.example.encryptednotebook.Cipher.decryptMsg;
import static com.example.encryptednotebook.Cipher.encryptMsg;
import static com.example.encryptednotebook.Cipher.generateKey;
@ -57,5 +58,4 @@ public class SetPasswordActivity extends AppCompatActivity {
}
});
}
}

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".ChangePassword">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_change_password" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ChangePassword"
tools:showIn="@layout/activity_change_password">
<TextView
android:id="@+id/setPassLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stare hasło"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="200dp"/>
<EditText
android:id="@+id/oldPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@+id/setPassLabel"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/setPassLabel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nowe hasło"
app:layout_constraintTop_toBottomOf="@+id/oldPass"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<EditText
android:id="@+id/setPass1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@+id/setPassLabel2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<EditText
android:id="@+id/setPass2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@+id/setPass1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -3,4 +3,5 @@
<string name="action_settings">Settings</string>
<string name="title_activity_set_password">SetPasswordActivity</string>
<string name="title_activity_notebook">NotebookActivity</string>
<string name="title_activity_change_password">ChangePassword</string>
</resources>