From 9c6fa7689541f384310cb8532880a6e43d8a04ce Mon Sep 17 00:00:00 2001 From: s452641 Date: Tue, 30 Nov 2021 20:28:30 +0100 Subject: [PATCH] zarabotalo2 --- .../java/com/example/tasklist/Adding_tasks.kt | 9 +--- .../java/com/example/tasklist/ChCrypto.kt | 50 ------------------- .../java/com/example/tasklist/ChangePass.kt | 8 +-- .../java/com/example/tasklist/Cryption.kt | 5 -- .../java/com/example/tasklist/LoginPage.kt | 25 ++++++---- .../main/java/com/example/tasklist/SetPass.kt | 4 +- 6 files changed, 20 insertions(+), 81 deletions(-) delete mode 100644 app/src/main/java/com/example/tasklist/ChCrypto.kt 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 1d4d943..c12ce2e 100644 --- a/app/src/main/java/com/example/tasklist/Adding_tasks.kt +++ b/app/src/main/java/com/example/tasklist/Adding_tasks.kt @@ -78,13 +78,8 @@ class Adding_tasks : Fragment() { 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 + private fun getSecretKey(): SecretKeySpec { + return SecretKeySpec(getPass()?.removeRange(0, 32)?.toByteArray(), "AES") } } diff --git a/app/src/main/java/com/example/tasklist/ChCrypto.kt b/app/src/main/java/com/example/tasklist/ChCrypto.kt deleted file mode 100644 index 6986c46..0000000 --- a/app/src/main/java/com/example/tasklist/ChCrypto.kt +++ /dev/null @@ -1,50 +0,0 @@ -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 - - -object ChCrypto{ - @RequiresApi(Build.VERSION_CODES.O) - @JvmStatic fun aesEncrypt(v:String) = AES256.encrypt(v) - @RequiresApi(Build.VERSION_CODES.O) - @JvmStatic fun aesDecrypt(v:String) = AES256.decrypt(v) -} - -private object AES256{ - @RequiresApi(Build.VERSION_CODES.O) - private val encorder = Base64.getEncoder() - @RequiresApi(Build.VERSION_CODES.O) - private val decorder = Base64.getDecoder() - @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() - - if(secretKey.length != 32) throw RuntimeException("SecretKey length is not 32 chars") - val c = Cipher.getInstance("AES/CBC/PKCS5Padding") - val sk = SecretKeySpec(secretKey.toByteArray(Charsets.UTF_8), "AES") - val iv = IvParameterSpec(secretKey.substring(0, 16).toByteArray(Charsets.UTF_8)) - c.init(opmode, sk, iv) - return c - } - - @RequiresApi(Build.VERSION_CODES.O) - fun encrypt(str: String):String{ - val encrypted = cipher(Cipher.ENCRYPT_MODE).doFinal(str.toByteArray(Charsets.UTF_8)) - return String(encorder.encode(encrypted)) - } - - @RequiresApi(Build.VERSION_CODES.O) - fun decrypt(str:String):String{ - val byteStr = decorder.decode(str.toByteArray(Charsets.UTF_8)) - return String(cipher(Cipher.DECRYPT_MODE).doFinal(byteStr)) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/tasklist/ChangePass.kt b/app/src/main/java/com/example/tasklist/ChangePass.kt index 01ed995..2dcab23 100644 --- a/app/src/main/java/com/example/tasklist/ChangePass.kt +++ b/app/src/main/java/com/example/tasklist/ChangePass.kt @@ -25,10 +25,6 @@ private const val ARG_PARAM2 = "param2" class ChangePass : Fragment(){ private lateinit var binding: FragmentChangePassBinding - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - } - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -57,13 +53,13 @@ class ChangePass : Fragment(){ "Password cannot be similar to the previous password!", Toast.LENGTH_SHORT).show() } - newPass.length >= 4 -> { + newPass.length >= 8 -> { savePass(newPassHash) Toast.makeText(requireActivity(),"Password updated!",Toast.LENGTH_SHORT).show() findNavController().navigate(R.id.action_changePass_to_taskListFragment) } else -> { - Toast.makeText(requireActivity(),"Password too short,it should have at least 4 characters!",Toast.LENGTH_SHORT).show() + Toast.makeText(requireActivity(),"Password too short,it should have at least 8 characters!",Toast.LENGTH_SHORT).show() } } diff --git a/app/src/main/java/com/example/tasklist/Cryption.kt b/app/src/main/java/com/example/tasklist/Cryption.kt index c849482..7c2ff9f 100644 --- a/app/src/main/java/com/example/tasklist/Cryption.kt +++ b/app/src/main/java/com/example/tasklist/Cryption.kt @@ -26,9 +26,4 @@ class Cryption { 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/LoginPage.kt b/app/src/main/java/com/example/tasklist/LoginPage.kt index 4a3c528..d0e3098 100644 --- a/app/src/main/java/com/example/tasklist/LoginPage.kt +++ b/app/src/main/java/com/example/tasklist/LoginPage.kt @@ -3,6 +3,7 @@ package com.example.tasklist import android.content.Context import android.content.SharedPreferences import android.os.Bundle +import android.os.SystemClock.sleep import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -29,17 +30,14 @@ private const val ARG_PARAM2 = "param2" */ class LoginPage : Fragment(){ private lateinit var binding: FragmentLoginPageBinding - private var counter: Int = 0 + private var counter: Int = 1 - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { + ): View { binding = DataBindingUtil.inflate( inflater, R.layout.fragment_login_page, container, false @@ -63,19 +61,24 @@ class LoginPage : Fragment(){ findNavController().navigate(R.id.action_loginPage_to_taskListFragment) } else{ - 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) + if (counter%5 == 0 && counter != 0){ + counter = 1 + Toast.makeText(requireActivity(),"You have been timed out for 30 second!",Toast.LENGTH_LONG).show() + //toSleep() } 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() + counter++ + Toast.makeText(requireActivity(),"Your password is incorrect. Try again!",Toast.LENGTH_SHORT).show() } + } } + private fun toSleep(){ + sleep(5000) + } + fun getPass(): String? { val sharedPreference = requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE) diff --git a/app/src/main/java/com/example/tasklist/SetPass.kt b/app/src/main/java/com/example/tasklist/SetPass.kt index 836b7cd..eb7ee89 100644 --- a/app/src/main/java/com/example/tasklist/SetPass.kt +++ b/app/src/main/java/com/example/tasklist/SetPass.kt @@ -60,13 +60,13 @@ class SetPass : Fragment() { fun goToList() { val pass = binding.password.text.toString() - if (pass.length >= 4) { + if (pass.length >= 8) { savePass(pass) saveHashForKey(pass) //Toast.makeText(requireActivity(), "Password is set", Toast.LENGTH_SHORT).show() findNavController().navigate(R.id.action_setPass_to_taskListFragment) } else { - Toast.makeText(requireActivity(),"Password too short,it should have at least 4 characters!",Toast.LENGTH_SHORT).show() + Toast.makeText(requireActivity(),"Password too short,it should have at least 8 characters!",Toast.LENGTH_SHORT).show() findNavController().navigate(R.id.action_setPass_self) } }