diff --git a/.idea/misc.xml b/.idea/misc.xml index 9088d56..858d1da 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -11,6 +11,8 @@ + + diff --git a/app/src/main/java/com/example/tasklist/Adding_tasks.kt b/app/src/main/java/com/example/tasklist/Adding_tasks.kt index 03edd6e..1d4d943 100644 --- a/app/src/main/java/com/example/tasklist/Adding_tasks.kt +++ b/app/src/main/java/com/example/tasklist/Adding_tasks.kt @@ -1,21 +1,22 @@ package com.example.tasklist +import android.content.Context import android.os.Build import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.Toast import androidx.annotation.RequiresApi import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment import androidx.navigation.fragment.findNavController -import com.example.tasklist.data.DataSource import com.example.tasklist.data.Database import com.example.tasklist.data.Task import com.example.tasklist.databinding.FragmentAddingTasksBinding import java.text.SimpleDateFormat import java.util.* +import javax.crypto.spec.IvParameterSpec +import javax.crypto.spec.SecretKeySpec // TODO: Rename parameter arguments, choose names that match @@ -59,8 +60,10 @@ class Adding_tasks : Fragment() { val dateTask = simpleDateFormat.format(Date()) if (textTask != "") { - val crypting = ChCrypto - val taskCrypred = crypting.aesEncrypt(textTask) + val secretKey = getSecretKey() + val crypting = Cryption() + val iv = IvParameterSpec(ByteArray(16)) + val taskCrypred = crypting.encrypt(textTask, secretKey, iv) val taskToAdd = Task(taskCrypred, dateTask) Database.taskDao.addTask(taskToAdd) @@ -68,4 +71,20 @@ class Adding_tasks : Fragment() { findNavController().navigate(R.id.action_adding_tasks_to_taskListFragment) } + private fun getPass(): String? { + val sharedPreference = + requireActivity().getSharedPreferences("KEY", Context.MODE_PRIVATE) + val pass = sharedPreference.getString("KEY_SECRET", null) + return pass + } + + private fun getSecretKey(): SecretKeySpec{ +// val sharedPreference = +// requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE) +// val pass = sharedPreference.getString("STRING_KEY", "pass") +// Toast.makeText(requireActivity(),pass.toString(),Toast.LENGTH_LONG).show() + val secretKey = SecretKeySpec(getPass()?.removeRange(0,32)?.toByteArray(), "AES") + return secretKey + } + } diff --git a/app/src/main/java/com/example/tasklist/ChCrypto.kt b/app/src/main/java/com/example/tasklist/ChCrypto.kt index 7b4c422..6986c46 100644 --- a/app/src/main/java/com/example/tasklist/ChCrypto.kt +++ b/app/src/main/java/com/example/tasklist/ChCrypto.kt @@ -24,9 +24,7 @@ private object AES256{ @SuppressLint("RestrictedApi") @RequiresApi(Build.VERSION_CODES.O) private fun cipher(opmode:Int): Cipher { - val secretKey = "H+MbQeThWmZq4t7w!z%C&F)J@NcRfUjX" - val key = SetPass() //val secretKey = key.getKey() diff --git a/app/src/main/java/com/example/tasklist/ChangePass.kt b/app/src/main/java/com/example/tasklist/ChangePass.kt index cf0eb68..01ed995 100644 --- a/app/src/main/java/com/example/tasklist/ChangePass.kt +++ b/app/src/main/java/com/example/tasklist/ChangePass.kt @@ -44,22 +44,18 @@ class ChangePass : Fragment(){ fun goToList() { val hash = HashString() - val sharedPreference = - requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE) - val sharedPass = sharedPreference.getString("STRING_KEY", null) + val oldPass = hash.hashString(binding.passwordOld.text.toString()) val newPass = binding.passwordNew.text.toString() val newPassHash = hash.hashString(newPass) - - if(oldPass.equals(sharedPass,false)) { + if(oldPass.equals(getPass(),false)) { when { - newPassHash.equals(sharedPass,false) -> { + newPassHash.equals(getPass(),false) -> { Toast.makeText( requireActivity(), "Password cannot be similar to the previous password!", Toast.LENGTH_SHORT).show() - //findNavController().navigate(R.id.action_changePass_self) } newPass.length >= 4 -> { savePass(newPassHash) @@ -68,14 +64,12 @@ class ChangePass : Fragment(){ } else -> { Toast.makeText(requireActivity(),"Password too short,it should have at least 4 characters!",Toast.LENGTH_SHORT).show() - //findNavController().navigate(R.id.action_changePass_self) } } } else{ Toast.makeText(requireActivity(),"Password is incorrect, try again!",Toast.LENGTH_SHORT).show() - //findNavController().navigate(R.id.action_changePass_self) } } @@ -89,4 +83,11 @@ class ChangePass : Fragment(){ }.apply() } + fun getPass(): String? { + val sharedPreference = + requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE) + val pass = sharedPreference.getString("STRING_KEY", null) + return pass + } + } \ No newline at end of file diff --git a/app/src/main/java/com/example/tasklist/Cryption.kt b/app/src/main/java/com/example/tasklist/Cryption.kt new file mode 100644 index 0000000..c849482 --- /dev/null +++ b/app/src/main/java/com/example/tasklist/Cryption.kt @@ -0,0 +1,34 @@ +package com.example.tasklist + +import android.annotation.SuppressLint +import android.os.Build +import androidx.annotation.RequiresApi +import java.util.* +import javax.crypto.Cipher +import javax.crypto.spec.IvParameterSpec +import javax.crypto.spec.SecretKeySpec + +class Cryption { + @SuppressLint("GetInstance") + @RequiresApi(Build.VERSION_CODES.O) + fun decrypt(cipherText: String, key: SecretKeySpec, iv: IvParameterSpec): String { + val cipher = Cipher.getInstance("AES") + cipher.init(Cipher.DECRYPT_MODE, key, iv) + val plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText)) + return String(plainText) + } + + @SuppressLint("GetInstance") + @RequiresApi(Build.VERSION_CODES.O) + fun encrypt(inputText: String, key: SecretKeySpec, iv: IvParameterSpec): String { + val cipher = Cipher.getInstance("AES") + cipher.init(Cipher.ENCRYPT_MODE, key, iv) + val cipherText = cipher.doFinal(inputText.toByteArray()) + return Base64.getEncoder().encodeToString(cipherText) + } + +// val inputText = "abcdefghigklmnopqrstuvwxyz0123456789" +// val algorithm = "AES/CBC/PKCS5Padding" +// val key = SecretKeySpec("1234567890123456".toByteArray(), "AES") +// val iv = IvParameterSpec(ByteArray(16)) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/tasklist/HashString.kt b/app/src/main/java/com/example/tasklist/HashString.kt index 83b1d9f..b8570f7 100644 --- a/app/src/main/java/com/example/tasklist/HashString.kt +++ b/app/src/main/java/com/example/tasklist/HashString.kt @@ -3,12 +3,6 @@ package com.example.tasklist import java.security.MessageDigest class HashString { -// fun hashString(input: String): String { -// return MessageDigest -// .getInstance("SHA-256") -// .digest(input.toByteArray()) -// .fold("", { str, it -> str + "%02x".format(it) }) -// } fun hashString(input: String): String { return MessageDigest .getInstance("SHA-256") diff --git a/app/src/main/java/com/example/tasklist/LoginPage.kt b/app/src/main/java/com/example/tasklist/LoginPage.kt index 9ab3763..4a3c528 100644 --- a/app/src/main/java/com/example/tasklist/LoginPage.kt +++ b/app/src/main/java/com/example/tasklist/LoginPage.kt @@ -1,6 +1,7 @@ package com.example.tasklist import android.content.Context +import android.content.SharedPreferences import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -10,7 +11,11 @@ import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment import androidx.navigation.fragment.findNavController import com.example.tasklist.databinding.FragmentLoginPageBinding +import kotlinx.coroutines.delay +import kotlinx.coroutines.withTimeout +import java.security.SecureRandom import java.util.* +import javax.crypto.spec.SecretKeySpec // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER @@ -24,6 +29,7 @@ private const val ARG_PARAM2 = "param2" */ class LoginPage : Fragment(){ private lateinit var binding: FragmentLoginPageBinding + private var counter: Int = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -41,33 +47,45 @@ class LoginPage : Fragment(){ checkIfPassSet() + binding.login = this@LoginPage return binding.root } fun goToList() { - val sharedPreference = - requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE) - val hash = HashString() val insertedText = hash.hashString(binding.password.text.toString()) - if(insertedText.equals(sharedPreference.getString("STRING_KEY", null),false)){ + if(insertedText.equals(getPass(),false)) + { findNavController().navigate(R.id.action_loginPage_to_taskListFragment) } else{ - Toast.makeText(requireActivity(),"Your password is incorrect. Try again!",Toast.LENGTH_SHORT).show() - findNavController().navigate(R.id.action_loginPage_self) + if (counter<=5){ + counter += 1 + Toast.makeText(requireActivity(),"Your password is incorrect. Try again!",Toast.LENGTH_SHORT).show() + //Toast.makeText(requireActivity(),counter,Toast.LENGTH_SHORT).show() + //findNavController().navigate(R.id.action_loginPage_self) + } + else{ + Toast.makeText(requireActivity(),"You wrote incorrect password 5 times in a row. You bhave been timed out for 30 second!",Toast.LENGTH_LONG).show() + } + } } - - private fun checkIfPassSet(){ + fun getPass(): String? { val sharedPreference = requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE) - if(sharedPreference.getString("STRING_KEY", null) == null){ + val pass = sharedPreference.getString("STRING_KEY", null) + return pass + } + + private fun checkIfPassSet(){ + + if(getPass() == null){ findNavController().navigate(R.id.action_loginPage_to_setPass) } } diff --git a/app/src/main/java/com/example/tasklist/SaltGen.kt b/app/src/main/java/com/example/tasklist/SaltGen.kt deleted file mode 100644 index 7cce000..0000000 --- a/app/src/main/java/com/example/tasklist/SaltGen.kt +++ /dev/null @@ -1,25 +0,0 @@ -package com.example.tasklist - -import java.math.BigInteger -import java.security.SecureRandom - - - private fun generateSalt(): ByteArray{ - val sr = SecureRandom() - val salt = ByteArray(32) - sr.nextBytes(salt) - println("MOJ SALT{${toHex(salt)}}:") - return salt - } - - - private fun toHex(array: ByteArray): String { - val bi = BigInteger(1, array) - val hex = bi.toString(16) - val paddingLength = array.size * 2 - hex.length - return if (paddingLength > 0) { - String.format("%0" + paddingLength + "d", 0) + hex - } else { - hex - } - } \ No newline at end of file diff --git a/app/src/main/java/com/example/tasklist/SetPass.kt b/app/src/main/java/com/example/tasklist/SetPass.kt index e4806ca..836b7cd 100644 --- a/app/src/main/java/com/example/tasklist/SetPass.kt +++ b/app/src/main/java/com/example/tasklist/SetPass.kt @@ -1,5 +1,6 @@ package com.example.tasklist +import android.R.attr import android.content.Context import android.os.Bundle import androidx.fragment.app.Fragment @@ -18,6 +19,13 @@ import java.security.MessageDigest import java.security.SecureRandom import javax.crypto.KeyGenerator import javax.crypto.SecretKey +import android.R.attr.key +import javax.crypto.Cipher +import android.preference.PreferenceManager + +import android.content.SharedPreferences +import javax.crypto.spec.SecretKeySpec + // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER @@ -54,6 +62,7 @@ class SetPass : Fragment() { if (pass.length >= 4) { savePass(pass) + saveHashForKey(pass) //Toast.makeText(requireActivity(), "Password is set", Toast.LENGTH_SHORT).show() findNavController().navigate(R.id.action_setPass_to_taskListFragment) } else { @@ -64,7 +73,6 @@ class SetPass : Fragment() { private fun savePass(pass: String) { - saveSecretKey() val hash = HashString() val password = hash.hashString(pass) val sharedPreference = @@ -75,25 +83,14 @@ class SetPass : Fragment() { }.apply() } - - private fun saveSecretKey(){ - - val secretKey = "H+MbQeThWmZq4t7w!z%C&F)J@NcRfUjX" - + private fun saveHashForKey(pass: String) { + val hash = HashString() + val password = hash.hashString(pass) val sharedPreference = requireActivity().getSharedPreferences("KEY", Context.MODE_PRIVATE) val editor = sharedPreference.edit() - editor.putString("secretKey", secretKey).apply() - Toast.makeText(requireActivity(),getKey(),Toast.LENGTH_LONG).show() - } - - - - fun getKey(): String{ - val sharedPreference = - requireActivity().getSharedPreferences("KEY", Context.MODE_PRIVATE) - val sharedPass = sharedPreference.getString("secretKey", "DEFAULT") - val secretKey = sharedPass.toString() - return secretKey + editor.apply { + putString("KEY_SECRET", password) + }.apply() } } diff --git a/app/src/main/java/com/example/tasklist/ui/adapters/TaskAdapter.kt b/app/src/main/java/com/example/tasklist/ui/adapters/TaskAdapter.kt index f823147..0be15ff 100644 --- a/app/src/main/java/com/example/tasklist/ui/adapters/TaskAdapter.kt +++ b/app/src/main/java/com/example/tasklist/ui/adapters/TaskAdapter.kt @@ -1,5 +1,6 @@ package com.example.tasklist.ui.adapters +import android.content.Context import android.os.Build import android.view.LayoutInflater import android.view.ViewGroup @@ -8,10 +9,15 @@ import androidx.databinding.DataBindingUtil import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView -import com.example.tasklist.ChCrypto +import com.example.tasklist.Adding_tasks +import com.example.tasklist.Cryption +import com.example.tasklist.LoginPage import com.example.tasklist.R import com.example.tasklist.data.Task import com.example.tasklist.databinding.ItemTaskBinding +import com.example.tasklist.ui.choose.list.TaskListFragment +import javax.crypto.spec.IvParameterSpec +import javax.crypto.spec.SecretKeySpec class TaskAdapter internal constructor( @@ -54,9 +60,11 @@ class TaskAdapter internal constructor( interface TaskItemClickListener { @RequiresApi(Build.VERSION_CODES.O) fun chooseTask(task: Task): String { - val crypting = ChCrypto - val taskCrypred = crypting.aesDecrypt(task.name) - return taskCrypred + val iv = IvParameterSpec(ByteArray(16)) + val secretKey = SecretKeySpec(TaskListFragment().getPass()?.removeRange(0,32)?.toByteArray(), "AES") + val crypting = Cryption() + + return crypting.decrypt(task.name, secretKey, iv) } } diff --git a/app/src/main/java/com/example/tasklist/ui/choose/list/TaskListFragment.kt b/app/src/main/java/com/example/tasklist/ui/choose/list/TaskListFragment.kt index e94c6d4..2278016 100644 --- a/app/src/main/java/com/example/tasklist/ui/choose/list/TaskListFragment.kt +++ b/app/src/main/java/com/example/tasklist/ui/choose/list/TaskListFragment.kt @@ -13,9 +13,7 @@ import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.findNavController -import com.example.tasklist.ChCrypto -import com.example.tasklist.MainApplication -import com.example.tasklist.R +import com.example.tasklist.* import com.example.tasklist.data.DataSource import com.example.tasklist.data.Database import com.example.tasklist.data.Repository @@ -24,6 +22,8 @@ import com.example.tasklist.data.dao.TaskDao import com.example.tasklist.databinding.FragmentTaskListBinding import com.example.tasklist.ui.adapters.TaskAdapter import com.example.tasklist.ui.adapters.TaskItemClickListener +import javax.crypto.spec.IvParameterSpec +import javax.crypto.spec.SecretKeySpec /** * A simple [Fragment] subclass. @@ -66,15 +66,22 @@ class TaskListFragment : Fragment(), TaskItemClickListener { @RequiresApi(Build.VERSION_CODES.O) override fun chooseTask(task: Task): String { - val crypting = ChCrypto - val taskCrypred = crypting.aesDecrypt(task.name) -// val taskToast = Toast.makeText(activity, "Zostało wybrane zadanie $taskCrypred", Toast.LENGTH_LONG) -// taskToast.show() - return taskCrypred + val iv = IvParameterSpec(ByteArray(16)) + val secretKey = SecretKeySpec(getPass()?.removeRange(0,32)?.toByteArray(), "AES") + val crypting = Cryption() + return crypting.decrypt(task.name, secretKey, iv) + //Toast.makeText(activity, "Zostało wybrane zadanie $taskCrypred", Toast.LENGTH_LONG).show() // val taskToast = Toast.makeText(activity, "Zostało wybrane zadanie ${task.name}", Toast.LENGTH_LONG) } + fun getPass(): String? { + val sharedPreference = + requireActivity().getSharedPreferences("KEY", Context.MODE_PRIVATE) + val pass = sharedPreference.getString("KEY_SECRET", null) + return pass + } + fun addTask(){ findNavController().navigate(R.id.action_taskListFragment_to_adding_tasks) } diff --git a/app/src/main/res/layout/item_task.xml b/app/src/main/res/layout/item_task.xml index b9253f2..f732919 100644 --- a/app/src/main/res/layout/item_task.xml +++ b/app/src/main/res/layout/item_task.xml @@ -10,16 +10,6 @@ - - - - - - + app:layout_constraintVertical_bias="1.0" + tools:layout_editor_absoluteX="44dp" /> \ No newline at end of file