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"?>
<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" />
</component>
<component name="ProjectType">

View File

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

View File

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

View File

@ -1,21 +1,52 @@
package com.zadanie1.mw;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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 {
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
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -33,17 +64,16 @@ public class FirstPassword extends Activity {
public void onClick(View arg0) {
if (newpassword.getText().toString().equals(renewpassword.getText().toString())) {
String text = newpassword.getText().toString();
String text = SHA512(newpassword.getText().toString());
FileOutputStream fos = null;
Toast toast = Toast.makeText(getApplicationContext(),
"Password has been changed",
Toast.LENGTH_SHORT);
toast.show();
try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes());
Toast toast = Toast.makeText(getApplicationContext(),
"Password has been changed",
Toast.LENGTH_SHORT);
toast.show();
} catch (FileNotFoundException e) {
e.printStackTrace();

View File

@ -1,6 +1,7 @@
package com.zadanie1.mw;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
@ -9,15 +10,21 @@ import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class FormPassword extends Activity {
private static final String FILE_NAME = "passwd.txt";
private static String input;
public String readFile(String filename) throws IOException {
@ -40,6 +47,56 @@ public class FormPassword extends Activity {
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
public void onCreate(Bundle savedInstanceState) {
@ -61,11 +118,17 @@ public class FormPassword extends Activity {
final String filename = getFilesDir() + "/passwd.txt";
File file = new File(filename);
input = currentpassword.getText().toString();
String SHAinput = unSHA512(input);
currentpassword.setText("");
String password = null;
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())) {
String text = newpassword.getText().toString();
String text = SHA512(newpassword.getText().toString());
FileOutputStream fos = null;
Toast toast = Toast.makeText(getApplicationContext(),
@ -106,6 +169,7 @@ public class FormPassword extends Activity {
} catch (IOException e) {
e.printStackTrace();
}
currentpassword.getText().clear();
newpassword.getText().clear();
renewpassword.getText().clear();

View File

@ -1,10 +1,12 @@
package com.zadanie1.mw;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import androidx.biometric.BiometricPrompt;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
@ -15,6 +17,10 @@ import android.widget.Toast;
import java.io.File;
import java.io.FileReader;
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 {
@ -24,6 +30,7 @@ public class MainActivity extends AppCompatActivity {
private EditText textPassword;
private View viewPassword;
private static final String filename = "passwd.txt";
private static String input;
public String readFile(String filename) throws IOException {
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -63,13 +106,15 @@ public class MainActivity extends AppCompatActivity {
FirstPassword.class);
startActivity(myIntent3);
}
else { showBiometricPrompt();
}
response = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
restartpassword = (Button) findViewById(R.id.restartpassword);
textPassword = findViewById(R.id.editText);
restartpassword.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this,
@ -83,39 +128,99 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onClick(View view) {
/*response.setText(textPassword.getText());*/
File file = new File(filename);
try {
if (isFileExists(file)) {
if (textPassword.getText().toString().equals(readFile(filename).toString())) {
Toast toast = Toast.makeText(getApplicationContext(),
"Password is correct!",
Toast.LENGTH_SHORT);
toast.show();
Intent myIntent2 = new Intent(MainActivity.this,
Noto.class);
startActivity(myIntent2);
try {
input = textPassword.getText().toString();
String SHAinput = unSHA512(input);
textPassword.setText("");
String password = readFile(filename).toString();
} 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);
}
if (isFileExists(file)) {
} catch(IOException e){
e.printStackTrace();
}
if(SHAinput.equals(password)){
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();
}
});
}
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;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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 {
EditText ChangeNote;
Button SaveNote;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -31,58 +50,91 @@ public class Noto extends AppCompatActivity {
ChangeNote = (EditText) findViewById(R.id.changeNote);
SaveNote = (Button) findViewById(R.id.SaveNote);
FileInputStream fis = null;
FileInputStream fis = null;
password = MainActivity.getInput();
try {
fis = openFileInput(FILE_NAME);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer();
String text;
try {
FileInputStream fileInputStream = null;
File file = new File(getFilesDir() + "/" + FILE_NAME);
byte[] ciphertext = new byte[(int) file.length()];
fileInputStream = new FileInputStream(file);
fileInputStream.read(ciphertext);
while ((text = br.readLine()) != null) {
sb.append(text).append("\n");
}
ChangeNote.setText(ciphertext.toString());
if (sb.toString() != null) {
ChangeNote.setText(sb.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
keyMultiply = password + soil + password + soil + soil + password + soil;
key = keyMultiply.substring(4, 20);
initVector = keyMultiply.substring(7, 23);
byte[] bytesKey = key.getBytes("UTF-8");
byte[] bytesIV = initVector.getBytes("UTF-8");
IvParameterSpec iv = new IvParameterSpec(bytesIV);
SecretKeySpec sKeySpec = new SecretKeySpec(bytesKey, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, iv);
String plaintext = new String(cipher.doFinal(ciphertext), "UTF-8");
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 {
try {
String text = ChangeNote.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text.getBytes());
keyMultiply = password + soil + password + soil + soil + password + soil;
key = keyMultiply.substring(4, 20);
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();*/
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME, Toast.LENGTH_LONG).show();
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null){
fos.close();
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} 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 {
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
// in the individual module build.gradle files
}