Safe password

This commit is contained in:
Wiktor Bałaban 2019-11-19 18:21:33 +01:00
parent 03ab902051
commit 720d5e54c1
4 changed files with 118 additions and 83 deletions

View File

@ -0,0 +1,67 @@
package com.example.encryptednotebook;
import android.content.Context;
import android.preference.PreferenceManager;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class Cipher {
public static SecretKey generateKey(String password, Context ctx)
throws NoSuchAlgorithmException, InvalidKeySpecException {
//new PBEKeySpec(password, salt, 10, 128)
String saltString = PreferenceManager.getDefaultSharedPreferences(ctx).getString("SALT", null);
byte[] saltBytes;
try {
saltBytes = saltString.getBytes();
} catch (NullPointerException e) {
e.printStackTrace();
saltBytes = "".getBytes();
}
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), saltBytes, 10, 256);
return new SecretKeySpec(SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512").generateSecret(keySpec).getEncoded(), "AES");
}
public static String encryptMsg(String message, SecretKey secret)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
/* Encrypt the message. */
javax.crypto.Cipher cipher = null;
cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5PADDING");
AlgorithmParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, secret, ivSpec);
byte[] utfMessageBytes = message.getBytes(StandardCharsets.UTF_8);
//byte[] base64MessageBytes = Base64.getUrlDecoder().decode(utfMessageBytes);
byte[] cipherText = cipher.doFinal(utfMessageBytes);
String result = Base64.getEncoder().encodeToString(cipherText);
return result;
}
public static String decryptMsg(String message, SecretKey secret)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
/* Decrypt the message, given derived encContentValues and initialization vector. */
javax.crypto.Cipher cipher = null;
cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5PADDING");
AlgorithmParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, secret, ivSpec);
byte[] messageBytes = Base64.getDecoder().decode(message);//message.getBytes(StandardCharsets.UTF_8);
byte[] decryptedBytes = cipher.doFinal(messageBytes);
String decryptString = new String(decryptedBytes, StandardCharsets.UTF_8);
return decryptString;
}
}

View File

@ -21,6 +21,11 @@ import android.widget.Toast;
import java.math.BigInteger;
import java.security.SecureRandom;
import javax.crypto.SecretKey;
import static com.example.encryptednotebook.Cipher.decryptMsg;
import static com.example.encryptednotebook.Cipher.generateKey;
public class MainActivity extends AppCompatActivity {
public static final int SET_PASSWORD_REQUEST = 1;
@ -57,15 +62,23 @@ public class MainActivity extends AppCompatActivity {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText password = findViewById(R.id.password);
String passValue = password.getText().toString();
String savedPassValue = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("PASSWORD", null);
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 {
try {
EditText password = findViewById(R.id.password);
String passValue = password.getText().toString();
String savedPassValue = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("PASSWORD", null);
SecretKey secretKey = generateKey(passValue, getApplicationContext());
String savedPassValueDecrypted = decryptMsg(savedPassValue, secretKey);
if (passValue.equals(savedPassValueDecrypted)) {
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();
}
}catch(Exception e){
e.printStackTrace();
Snackbar.make(view, "Złe hasło", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}

View File

@ -1,37 +1,21 @@
package com.example.encryptednotebook;
import android.content.Intent;
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 java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.util.Base64;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
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.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import static com.example.encryptednotebook.Cipher.decryptMsg;
import static com.example.encryptednotebook.Cipher.encryptMsg;
import static com.example.encryptednotebook.Cipher.generateKey;
public class NotebookActivity extends AppCompatActivity {
@ -46,7 +30,7 @@ public class NotebookActivity extends AppCompatActivity {
String savedPassValue = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("PASSWORD", null);
if (savedPassValue != null) {
try {
SecretKey secretKey = generateKey(savedPassValue);
SecretKey secretKey = generateKey(savedPassValue,getApplicationContext());
String encryptedText = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("TEXT", null);
//String decryptedText = decryptMsg(encryptedText.getBytes(),secretKey);
if (encryptedText != null) {
@ -70,9 +54,9 @@ public class NotebookActivity extends AppCompatActivity {
String savedPassValue = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("PASSWORD", null);
if (savedPassValue != null) {
try {
SecretKey secretKey = generateKey(savedPassValue);
SecretKey secretKey = generateKey(savedPassValue,getApplicationContext());
String encryptedText = encryptMsg(textValue, secretKey);
String ddddd = decryptMsg(encryptedText, secretKey);
//String ddddd = decryptMsg(encryptedText, secretKey);
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("TEXT", encryptedText).apply();
Snackbar.make(view, "Udało się zapisać", Snackbar.LENGTH_LONG)
@ -85,46 +69,6 @@ public class NotebookActivity extends AppCompatActivity {
});
}
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, 256);
return new SecretKeySpec(SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512").generateSecret(keySpec).getEncoded(), "AES");
}
public String encryptMsg(String message, SecretKey secret)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
/* Encrypt the message. */
Cipher cipher = null;
cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
AlgorithmParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
cipher.init(Cipher.ENCRYPT_MODE, secret, ivSpec);
byte[] utfMessageBytes = message.getBytes(StandardCharsets.UTF_8);
//byte[] base64MessageBytes = Base64.getUrlDecoder().decode(utfMessageBytes);
byte[] cipherText = cipher.doFinal(utfMessageBytes);
String result = Base64.getEncoder().encodeToString(cipherText);
return result;
}
public String decryptMsg(String message, 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/CBC/PKCS5PADDING");
AlgorithmParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
cipher.init(Cipher.DECRYPT_MODE, secret, ivSpec);
byte[] messageBytes = Base64.getDecoder().decode(message);//message.getBytes(StandardCharsets.UTF_8);
byte[] decryptedBytes = cipher.doFinal(messageBytes);
String decryptString = new String(decryptedBytes, StandardCharsets.UTF_8);
return decryptString;
}
}

View File

@ -15,6 +15,11 @@ import android.preference.PreferenceManager;
import android.view.View;
import android.widget.EditText;
import javax.crypto.SecretKey;
import static com.example.encryptednotebook.Cipher.encryptMsg;
import static com.example.encryptednotebook.Cipher.generateKey;
public class SetPasswordActivity extends AppCompatActivity {
@Override
@ -33,12 +38,18 @@ public class SetPasswordActivity extends AppCompatActivity {
EditText setPass2 = findViewById(R.id.setPass2);
String setPass2Value = setPass2.getText().toString();
if (setPass1Value.equals(setPass2Value)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
prefs.edit().putBoolean("PASSWORD_CREATED", true).apply();
prefs.edit().putString("PASSWORD", setPass1Value).apply();
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
try {
SecretKey secretKey = generateKey(setPass2Value, getApplicationContext());
String encryptedText = encryptMsg(setPass2Value, secretKey);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
prefs.edit().putBoolean("PASSWORD_CREATED", true).apply();
prefs.edit().putString("PASSWORD", encryptedText).apply();
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
}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();