zarabotalo2

This commit is contained in:
s452641 2021-11-30 20:28:30 +01:00
parent 10079517ca
commit 9c6fa76895
6 changed files with 20 additions and 81 deletions

View File

@ -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")
}
}

View File

@ -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))
}
}

View File

@ -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()
}
}

View File

@ -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))
}

View File

@ -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)

View File

@ -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)
}
}