Added en/de-cryption, fingerprint.

This commit is contained in:
Marcin Wozniak 2019-11-16 23:21:46 +01:00
parent 7f9d5898f5
commit d506c71afa
8 changed files with 352 additions and 94 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="JDK" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View File

@ -16,13 +16,20 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
} }
} }
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
buildToolsVersion = '29.0.2'
} }
dependencies { dependencies {
implementation 'androidx.biometric:biometric:1.0.0'
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
} }

View File

@ -3,6 +3,7 @@
package="com.zadanie1.mw"> package="com.zadanie1.mw">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.fingerprint" android:required="true" />
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -1,21 +1,52 @@
package com.zadanie1.mw; package com.zadanie1.mw;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.app.Activity; import android.app.Activity;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class FirstPassword extends Activity { public class FirstPassword extends Activity {
private static final String FILE_NAME = "passwd.txt"; private static final String FILE_NAME = "passwd.txt";
public static String SHA512(String s)
{
MessageDigest digest;
String generatedPassword = null;
String salt = "cnsakn";
try
{
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(salt.getBytes(StandardCharsets.UTF_8));
byte[] bytes = md.digest(s.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedPassword;
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -33,17 +64,16 @@ public class FirstPassword extends Activity {
public void onClick(View arg0) { public void onClick(View arg0) {
if (newpassword.getText().toString().equals(renewpassword.getText().toString())) { if (newpassword.getText().toString().equals(renewpassword.getText().toString())) {
String text = newpassword.getText().toString(); String text = SHA512(newpassword.getText().toString());
FileOutputStream fos = null; FileOutputStream fos = null;
Toast toast = Toast.makeText(getApplicationContext(),
"Password has been changed",
Toast.LENGTH_SHORT);
toast.show();
try { try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE); fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes()); fos.write(text.getBytes());
Toast toast = Toast.makeText(getApplicationContext(),
"Password has been changed",
Toast.LENGTH_SHORT);
toast.show();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -1,6 +1,7 @@
package com.zadanie1.mw; package com.zadanie1.mw;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.app.Activity; import android.app.Activity;
import android.view.View; import android.view.View;
@ -9,15 +10,21 @@ import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.RequiresApi;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class FormPassword extends Activity { public class FormPassword extends Activity {
private static final String FILE_NAME = "passwd.txt"; private static final String FILE_NAME = "passwd.txt";
private static String input;
public String readFile(String filename) throws IOException { public String readFile(String filename) throws IOException {
@ -40,6 +47,56 @@ public class FormPassword extends Activity {
return content; return content;
} }
public static String SHA512(String s)
{
MessageDigest digest;
String generatedPassword = null;
String salt = "cnsakn";
try
{
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(salt.getBytes(StandardCharsets.UTF_8));
byte[] bytes = md.digest(s.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedPassword;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private String unSHA512(String passwordToHash)
{
String generatedPassword = null;
String salt = "cnsakn";
try
{
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(salt.getBytes(StandardCharsets.UTF_8));
byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for(int i=0; i< bytes.length ;i++)
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedPassword;
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -61,11 +118,17 @@ public class FormPassword extends Activity {
final String filename = getFilesDir() + "/passwd.txt"; final String filename = getFilesDir() + "/passwd.txt";
File file = new File(filename); File file = new File(filename);
input = currentpassword.getText().toString();
String SHAinput = unSHA512(input);
currentpassword.setText("");
String password = null;
try { try {
if (currentpassword.getText().toString().equals(readFile(filename).toString())) { password = readFile(filename).toString();
if(SHAinput.equals(password)){
if (newpassword.getText().toString().equals(renewpassword.getText().toString())) { if (newpassword.getText().toString().equals(renewpassword.getText().toString())) {
String text = newpassword.getText().toString(); String text = SHA512(newpassword.getText().toString());
FileOutputStream fos = null; FileOutputStream fos = null;
Toast toast = Toast.makeText(getApplicationContext(), Toast toast = Toast.makeText(getApplicationContext(),
@ -106,6 +169,7 @@ public class FormPassword extends Activity {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
currentpassword.getText().clear(); currentpassword.getText().clear();
newpassword.getText().clear(); newpassword.getText().clear();
renewpassword.getText().clear(); renewpassword.getText().clear();

View File

@ -1,10 +1,12 @@
package com.zadanie1.mw; package com.zadanie1.mw;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricPrompt;
import android.Manifest;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Handler;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
@ -15,6 +17,10 @@ import android.widget.Toast;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Executor;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@ -24,6 +30,7 @@ public class MainActivity extends AppCompatActivity {
private EditText textPassword; private EditText textPassword;
private View viewPassword; private View viewPassword;
private static final String filename = "passwd.txt"; private static final String filename = "passwd.txt";
private static String input;
public String readFile(String filename) throws IOException { public String readFile(String filename) throws IOException {
String content = null; String content = null;
@ -50,6 +57,42 @@ public class MainActivity extends AppCompatActivity {
} }
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private String unSHA512(String passwordToHash)
{
String generatedPassword = null;
String salt = "cnsakn";
try
{
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(salt.getBytes(StandardCharsets.UTF_8));
byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for(int i=0; i< bytes.length ;i++)
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedPassword;
}
private Handler handler = new Handler();
private Executor executor = new Executor() {
@Override
public void execute(Runnable command) {
handler.post(command);
}
};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -63,13 +106,15 @@ public class MainActivity extends AppCompatActivity {
FirstPassword.class); FirstPassword.class);
startActivity(myIntent3); startActivity(myIntent3);
} }
else { showBiometricPrompt();
}
response = (TextView) findViewById(R.id.textView); response = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button); button = (Button) findViewById(R.id.button);
restartpassword = (Button) findViewById(R.id.restartpassword); restartpassword = (Button) findViewById(R.id.restartpassword);
textPassword = findViewById(R.id.editText); textPassword = findViewById(R.id.editText);
restartpassword.setOnClickListener(new OnClickListener() { restartpassword.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) { public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this, Intent myIntent = new Intent(MainActivity.this,
@ -83,39 +128,99 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
/*response.setText(textPassword.getText());*/
File file = new File(filename); File file = new File(filename);
try { try {
if (isFileExists(file)) { input = textPassword.getText().toString();
if (textPassword.getText().toString().equals(readFile(filename).toString())) { String SHAinput = unSHA512(input);
Toast toast = Toast.makeText(getApplicationContext(), textPassword.setText("");
"Password is correct!", String password = readFile(filename).toString();
Toast.LENGTH_SHORT);
toast.show();
Intent myIntent2 = new Intent(MainActivity.this,
Noto.class);
startActivity(myIntent2);
} else { if (isFileExists(file)) {
Toast toast = Toast.makeText(getApplicationContext(),
"Password is NOT correct",
Toast.LENGTH_SHORT);
toast.show();
}
} else {
Intent myIntent3 = new Intent(MainActivity.this,
FirstPassword.class);
startActivity(myIntent3);
}
} catch(IOException e){ if(SHAinput.equals(password)){
e.printStackTrace(); Toast toast = Toast.makeText(getApplicationContext(),
} "Password is correct!",
Toast.LENGTH_SHORT);
toast.show();
Intent myIntent2 = new Intent(MainActivity.this,
Noto.class);
startActivity(myIntent2);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"Password is NOT correct",
Toast.LENGTH_SHORT);
toast.show();
}
} else {
Intent myIntent3 = new Intent(MainActivity.this,
FirstPassword.class);
startActivity(myIntent3);
}
} catch(IOException e){
e.printStackTrace();
}
textPassword.getText().clear(); textPassword.getText().clear();
} }
}); });
} }
public static String getInput() {
return input;
}
private void showBiometricPrompt() {
BiometricPrompt.PromptInfo promptInfo =
new BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using your biometric credential")
.setNegativeButtonText("Cancel")
.build();
BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this,
executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode,
@NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
Toast.makeText(getApplicationContext(),
"Authentication error: " + errString, Toast.LENGTH_SHORT)
.show();
}
@Override
public void onAuthenticationSucceeded(
@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
BiometricPrompt.CryptoObject authenticatedCryptoObject =
result.getCryptoObject();
// User has verified the signature, cipher, or message
// authentication code (MAC) associated with the crypto object,
// so you can use it in your app's crypto-driven workflows.
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Toast.makeText(getApplicationContext(), "Authentication failed",
Toast.LENGTH_SHORT)
.show();
try {
Thread.sleep(1000);
System.exit(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// Displays the "log in" prompt.
biometricPrompt.authenticate(promptInfo);
}
} }

View File

@ -1,29 +1,48 @@
package com.zadanie1.mw; package com.zadanie1.mw;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Noto extends AppCompatActivity { public class Noto extends AppCompatActivity {
EditText ChangeNote; EditText ChangeNote;
Button SaveNote; Button SaveNote;
private static final String FILE_NAME = "notes.txt"; private static final String FILE_NAME = "notes.txt";
static String text;
private EditText textInputNotes;
private static String keyMultiply;
private static String key;
private static String initVector;
protected static String password;
protected static String soil= "yWaDPPsFMXxXwEJsTcf42";
int pom=0;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -31,58 +50,91 @@ public class Noto extends AppCompatActivity {
ChangeNote = (EditText) findViewById(R.id.changeNote); ChangeNote = (EditText) findViewById(R.id.changeNote);
SaveNote = (Button) findViewById(R.id.SaveNote); SaveNote = (Button) findViewById(R.id.SaveNote);
FileInputStream fis = null; FileInputStream fis = null;
password = MainActivity.getInput();
try { try {
fis = openFileInput(FILE_NAME); FileInputStream fileInputStream = null;
InputStreamReader isr = new InputStreamReader(fis); File file = new File(getFilesDir() + "/" + FILE_NAME);
BufferedReader br = new BufferedReader(isr); byte[] ciphertext = new byte[(int) file.length()];
StringBuffer sb = new StringBuffer(); fileInputStream = new FileInputStream(file);
String text; fileInputStream.read(ciphertext);
while ((text = br.readLine()) != null) { ChangeNote.setText(ciphertext.toString());
sb.append(text).append("\n");
}
if (sb.toString() != null) { keyMultiply = password + soil + password + soil + soil + password + soil;
ChangeNote.setText(sb.toString()); key = keyMultiply.substring(4, 20);
} initVector = keyMultiply.substring(7, 23);
} catch (FileNotFoundException e) {
e.printStackTrace(); byte[] bytesKey = key.getBytes("UTF-8");
} catch (IOException e) { byte[] bytesIV = initVector.getBytes("UTF-8");
e.printStackTrace(); IvParameterSpec iv = new IvParameterSpec(bytesIV);
} finally { SecretKeySpec sKeySpec = new SecretKeySpec(bytesKey, "AES");
if (fis != null){
try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
fis.close(); cipher.init(Cipher.DECRYPT_MODE, sKeySpec, iv);
} catch (IOException e) { String plaintext = new String(cipher.doFinal(ciphertext), "UTF-8");
e.printStackTrace(); ChangeNote.setText(plaintext);
}
}
} } catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }
public void save (View v) throws IOException { public void save (View v) throws IOException {
try {
String text = ChangeNote.getText().toString(); String text = ChangeNote.getText().toString();
FileOutputStream fos = null; FileOutputStream fos = null;
try { keyMultiply = password + soil + password + soil + soil + password + soil;
fos = openFileOutput(FILE_NAME, MODE_PRIVATE); key = keyMultiply.substring(4, 20);
fos.write(text.getBytes()); initVector = keyMultiply.substring(7, 23);
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
/*ChangeNote.getText().clear();*/ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME, Toast.LENGTH_LONG).show(); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(text.getBytes());
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(encrypted);
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME, Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } catch (NoSuchAlgorithmException e) {
if (fos != null){ e.printStackTrace();
fos.close(); } catch (InvalidKeyException e) {
} e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} }
} }
} }

View File

@ -7,8 +7,7 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.5.1' classpath 'com.android.tools.build:gradle:3.5.2'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
} }