diff --git a/app/build.gradle b/app/build.gradle index f3c052d..3bbdc1a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,7 +8,7 @@ android { defaultConfig { applicationId "com.example.bsm_fingerptint_tutorial" - minSdk 24 + minSdk 26 targetSdk 33 versionCode 1 versionName "1.0" diff --git a/app/src/main/java/com/example/bsm_fingerptint_tutorial/MainActivity.java b/app/src/main/java/com/example/bsm_fingerptint_tutorial/MainActivity.java index 63402c3..ec34fbb 100644 --- a/app/src/main/java/com/example/bsm_fingerptint_tutorial/MainActivity.java +++ b/app/src/main/java/com/example/bsm_fingerptint_tutorial/MainActivity.java @@ -71,18 +71,6 @@ public class MainActivity extends AppCompatActivity { public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { super.onAuthenticationSucceeded(result); - String plaintext = "Tekst do zaszyforwania"; - byte[] encryptedInfo; - - try { - encryptedInfo = result.getCryptoObject().getCipher().doFinal( - plaintext.getBytes(Charset.defaultCharset())); - } catch (GeneralSecurityException e) { - throw new RuntimeException(e); - } - - Log.d("MY_LOGS", "Success! Encrypted txt: " + Arrays.toString(encryptedInfo)); - Toast.makeText(getApplicationContext(),"Authentication succeeded!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(getApplicationContext(), NotepadActivity.class); @@ -104,54 +92,13 @@ public class MainActivity extends AppCompatActivity { .setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG) .build(); - - try { - generateSecretKey(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) - .setBlockModes(KeyProperties.BLOCK_MODE_CBC) - .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7) - .setUserAuthenticationRequired(true) - .setInvalidatedByBiometricEnrollment(true) - .build() - ); - } catch (GeneralSecurityException e) { - throw new RuntimeException(e); - } - - btnLogIn.setOnClickListener(view -> { - try { - Cipher cipher = getCipher(); - SecretKey secretKey = getSecretKey(); - cipher.init(Cipher.ENCRYPT_MODE, secretKey); - biometricPrompt.authenticate(promptInfo, new BiometricPrompt.CryptoObject(cipher)); - } catch (GeneralSecurityException | IOException e) { - throw new RuntimeException(e); - } + biometricPrompt.authenticate(promptInfo); + }); } - - - private void generateSecretKey(KeyGenParameterSpec keyGenParameterSpec) throws GeneralSecurityException { - KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "AndroidKeyStore"); - keyGenerator.init(keyGenParameterSpec); - keyGenerator.generateKey(); - } - - private SecretKey getSecretKey() throws GeneralSecurityException, IOException { - KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); - - keyStore.load(null); - return ((SecretKey)keyStore.getKey(KEY_NAME, null)); - } - - private Cipher getCipher() throws GeneralSecurityException{ - return Cipher.getInstance("AES/CBC/PKCS7Padding"); - //KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7 - } - - } diff --git a/app/src/main/java/com/example/bsm_fingerptint_tutorial/NotepadActivity.java b/app/src/main/java/com/example/bsm_fingerptint_tutorial/NotepadActivity.java index 9e91494..c61ecfd 100644 --- a/app/src/main/java/com/example/bsm_fingerptint_tutorial/NotepadActivity.java +++ b/app/src/main/java/com/example/bsm_fingerptint_tutorial/NotepadActivity.java @@ -2,12 +2,15 @@ package com.example.bsm_fingerptint_tutorial; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; +import androidx.biometric.BiometricPrompt; import androidx.lifecycle.LifecycleCoroutineScope; import android.annotation.SuppressLint; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.security.keystore.KeyGenParameterSpec; +import android.security.keystore.KeyProperties; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -17,16 +20,31 @@ import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; +import java.io.IOException; +import java.nio.charset.Charset; import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.security.SecureRandom; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Base64; import java.util.List; +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.crypto.spec.IvParameterSpec; + public class NotepadActivity extends AppCompatActivity { private List noteList; private LinearLayout notesContainer; private static final String SHARED_NAME_NOTES = "Notatki"; + private static final String KEY_NAME = "nazwaKluczaa"; + + + Button buttonLogout, buttonAddNote; @Override protected void onCreate(Bundle savedInstanceState) { @@ -36,7 +54,11 @@ public class NotepadActivity extends AppCompatActivity { noteList = new ArrayList<>(); notesContainer = findViewById(R.id.notes_container); - loadNotesFromPreferencesToList(); + try { + loadNotesFromPreferencesToList(); + } catch (Exception e) { + throw new RuntimeException(e); + } displayNotes(); buttonLogout = findViewById(R.id.btn_logout); @@ -75,7 +97,13 @@ public class NotepadActivity extends AppCompatActivity { noteList.add(note); - saveNotesToPreferences("add"); + try { + saveNotesToPreferences("add"); + } catch (GeneralSecurityException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } createNoteView(note); Toast.makeText(NotepadActivity.this, "Note saved!", Toast.LENGTH_SHORT).show(); @@ -116,7 +144,13 @@ public class NotepadActivity extends AppCompatActivity { noteList.add(note); createNoteView(note); - saveNotesToPreferences("add"); + try { + saveNotesToPreferences("add"); + } catch (GeneralSecurityException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } }else { Toast.makeText(NotepadActivity.this, "Enter title and content!", Toast.LENGTH_SHORT).show(); @@ -130,44 +164,92 @@ public class NotepadActivity extends AppCompatActivity { alertDialog.show(); } - private void saveNotesToPreferences(String mode){ + private void saveNotesToPreferences(String mode) throws GeneralSecurityException, IOException { SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); - if (mode.equals("del")){ - int noteCount = sharedPreferences.getInt("_notecount_", 0); - for(int i=0; i