From a43e22728998e65e76cccd9dc66c872114af7b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Ba=C5=82aban?= Date: Tue, 12 Nov 2019 17:37:30 +0100 Subject: [PATCH] Not decrypting correctly --- .../encryptednotebook/MainActivity.java | 25 +++++- .../encryptednotebook/NotebookActivity.java | 87 ++++++++++++++++++- 2 files changed, 105 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/example/encryptednotebook/MainActivity.java b/app/src/main/java/com/example/encryptednotebook/MainActivity.java index 9f07275..27f27d4 100644 --- a/app/src/main/java/com/example/encryptednotebook/MainActivity.java +++ b/app/src/main/java/com/example/encryptednotebook/MainActivity.java @@ -18,6 +18,9 @@ import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; +import java.math.BigInteger; +import java.security.SecureRandom; + public class MainActivity extends AppCompatActivity { public static final int SET_PASSWORD_REQUEST = 1; @@ -30,8 +33,22 @@ public class MainActivity extends AppCompatActivity { setSupportActionBar(toolbar); TextView hello = findViewById(R.id.hello); hello.setText("Wpisz hasło"); + + String saltString = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("SALT", null); + if (saltString == null) { + String randomString = new BigInteger(130, new SecureRandom()).toString(32); + byte[] salt = new byte[8]; + try { + System.arraycopy(randomString.getBytes("UTF-8"), 0, salt, 0, 8); + PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("SALT", new String(salt)).apply(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + boolean isPassCreated = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("PASSWORD_CREATED", false); - if(!isPassCreated){ + if (!isPassCreated) { Intent pickContactIntent = new Intent(this, SetPasswordActivity.class); startActivityForResult(pickContactIntent, SET_PASSWORD_REQUEST); } @@ -43,9 +60,11 @@ public class MainActivity extends AppCompatActivity { EditText password = findViewById(R.id.password); String passValue = password.getText().toString(); String savedPassValue = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("PASSWORD", null); - if(passValue.equals(savedPassValue)) { + if (passValue.equals(savedPassValue)) { Snackbar.make(view, "Dobre hasło", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); + Intent intent = new Intent(getApplicationContext(), NotebookActivity.class); + startActivity(intent); } else { Snackbar.make(view, "Złe hasło", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); @@ -80,7 +99,7 @@ public class MainActivity extends AppCompatActivity { protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SET_PASSWORD_REQUEST) { - if(resultCode == Activity.RESULT_OK){ + if (resultCode == Activity.RESULT_OK) { Toast.makeText(getApplicationContext(), "Udało się ustawić hasło", Toast.LENGTH_SHORT).show(); } if (resultCode == Activity.RESULT_CANCELED) { diff --git a/app/src/main/java/com/example/encryptednotebook/NotebookActivity.java b/app/src/main/java/com/example/encryptednotebook/NotebookActivity.java index 968626f..6eef5d7 100644 --- a/app/src/main/java/com/example/encryptednotebook/NotebookActivity.java +++ b/app/src/main/java/com/example/encryptednotebook/NotebookActivity.java @@ -1,5 +1,6 @@ package com.example.encryptednotebook; +import android.content.Intent; import android.os.Bundle; import com.google.android.material.floatingactionbutton.FloatingActionButton; @@ -12,6 +13,22 @@ import android.preference.PreferenceManager; import android.view.View; import android.widget.EditText; +import java.io.UnsupportedEncodingException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.InvalidParameterSpecException; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.SecretKeySpec; + public class NotebookActivity extends AppCompatActivity { @Override @@ -20,19 +37,81 @@ public class NotebookActivity extends AppCompatActivity { setContentView(R.layout.activity_notebook); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); + final EditText text = findViewById(R.id.text); + + String savedPassValue = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("PASSWORD", null); + if (savedPassValue != null) { + try { + SecretKey secretKey = generateKey(savedPassValue); + String encryptedText = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("TEXT", null); + if (encryptedText != null) { + String noteText = decryptMsg(encryptedText.getBytes(), secretKey); + text.setText(noteText); + } else { + text.setText("Twoja notatka"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - EditText text = findViewById(R.id.text); String textValue = text.getText().toString(); - PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("TEXT", textValue).apply(); - Snackbar.make(view, "Udało się zapisać", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); + String savedPassValue = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("PASSWORD", null); + if (savedPassValue != null) { + try { + SecretKey secretKey = generateKey(savedPassValue); + String encryptedText = new String(encryptMsg(textValue, secretKey)); + PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("TEXT", encryptedText).apply(); + + Snackbar.make(view, "Udało się zapisać", Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + } catch (Exception e) { + e.printStackTrace(); + } + } } }); } + public SecretKey generateKey(String password) + throws NoSuchAlgorithmException, InvalidKeySpecException { + //new PBEKeySpec(password, salt, 10, 128) + String saltString = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("SALT", null); + byte[] saltBytes; + try { + saltBytes = saltString.getBytes(); + } catch (NullPointerException e) { + e.printStackTrace(); + saltBytes = "".getBytes(); + } + PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), saltBytes, 10, 128); + return new SecretKeySpec(SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512").generateSecret(keySpec).getEncoded(), "AES"); + } + + public byte[] encryptMsg(String message, SecretKey secret) + throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { + /* Encrypt the message. */ + Cipher cipher = null; + cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secret); + byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8")); + return cipherText; + } + + public String decryptMsg(byte[] cipherText, SecretKey secret) + throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { + /* Decrypt the message, given derived encContentValues and initialization vector. */ + Cipher cipher = null; + cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, secret); + String decryptString = new String(cipher.doFinal(cipherText), "UTF-8"); + return decryptString; + } + }