allmostEnAndDeCrypting

This commit is contained in:
s452641 2021-11-25 00:31:08 +01:00
parent 56a942edf6
commit cc7f3fbfef
14 changed files with 111 additions and 85 deletions

View File

@ -11,6 +11,11 @@
<entry key="..\:/Android Projects/TaskListA2/app/src/main/res/layout/item_task.xml" value="0.20520833333333333" />
<entry key="..\:/Android Projects/TaskListA2/app/src/main/res/layout/main_activity.xml" value="0.20520833333333333" />
<entry key="..\:/Android Projects/TaskListA2/app/src/main/res/layout/main_fragment.xml" value="0.20520833333333333" />
<entry key="..\:/Android Projects/TaskListA3/app/src/main/res/layout/fragment_change_pass.xml" value="0.2109375" />
<entry key="..\:/Android Projects/TaskListA3/app/src/main/res/layout/fragment_login_page.xml" value="0.2109375" />
<entry key="..\:/Android Projects/TaskListA3/app/src/main/res/layout/fragment_set_pass.xml" value="0.2109375" />
<entry key="..\:/Android Projects/TaskListA3/app/src/main/res/layout/fragment_task_list.xml" value="0.2210144927536232" />
<entry key="..\:/Android Projects/TaskListA3/app/src/main/res/layout/item_task.xml" value="0.2210144927536232" />
<entry key="..\:/AndroidStudio/TaskListA/app/src/main/res/drawable/bordershape.xml" value="0.22135416666666666" />
<entry key="..\:/AndroidStudio/TaskListA/app/src/main/res/layout/activity_login_page.xml" value="0.20520833333333333" />
<entry key="..\:/AndroidStudio/TaskListA/app/src/main/res/layout/fragment_activity_login_page.xml" value="0.2682291666666667" />

View File

@ -1,9 +1,11 @@
package com.example.tasklist
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
@ -13,6 +15,7 @@ import com.example.tasklist.databinding.FragmentAddingTasksBinding
import java.text.SimpleDateFormat
import java.util.*
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
@ -25,6 +28,7 @@ private const val ARG_PARAM2 = "param2"
*/
class Adding_tasks : Fragment() {
private lateinit var binding: FragmentAddingTasksBinding
override fun onCreate(savedInstanceState: Bundle?) {
@ -44,18 +48,20 @@ class Adding_tasks : Fragment() {
return binding.root
}
@RequiresApi(Build.VERSION_CODES.O)
fun goToTasks() {
var pattern = "dd-MM-yyyy hh:mm"
var simpleDateFormat = SimpleDateFormat(pattern, Locale("pl", "PL"))
val pattern = "dd-MM-yyyy hh:mm"
val simpleDateFormat = SimpleDateFormat(pattern, Locale("pl", "PL"))
val textTask = binding.editTextName.text.toString()
val dateTask = simpleDateFormat.format(Date())
if (textTask != "") {
val ttask = Task(textTask, dateTask)
Database.taskDao.addTask(ttask)
val crypting = ChCrypto
val taskCrypred = crypting.aesEncrypt(textTask)
val taskToAdd = Task(taskCrypred, dateTask)
Database.taskDao.addTask(taskToAdd)
}
findNavController().navigate(R.id.action_adding_tasks_to_taskListFragment)
}
}

View File

@ -0,0 +1,45 @@
package com.example.tasklist
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()
private fun cipher(opmode:Int): Cipher {
val secretKey = "H+MbQeThWmZq4t7w!z%C&F)J@NcRfUjX"
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

@ -1,22 +1,15 @@
package com.example.tasklist
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.DataBindingUtil.setContentView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.tasklist.databinding.FragmentChangePassBinding
import android.content.SharedPreferences
import android.net.wifi.WifiEnterpriseConfig
import android.widget.Toast
import java.lang.Exception
import java.math.BigInteger
import java.security.MessageDigest
// TODO: Rename parameter arguments, choose names that match
@ -34,11 +27,6 @@ class ChangePass : Fragment(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// val sharedPreference =
// requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE)
// val sharedPass = sharedPreference.getString("STRING_KEY", null)
// Toast.makeText(requireActivity(),sharedPass,Toast.LENGTH_SHORT).show()
}
override fun onCreateView(
@ -55,16 +43,16 @@ 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 = hashString(binding.passwordOld.text.toString())
val oldPass = hash.hashString(binding.passwordOld.text.toString())
var newPass = binding.passwordNew.text.toString()
if(oldPass.equals(sharedPass,false)) {
if (newPass.length >= 4){
newPass = hashString(newPass)
newPass = hash.hashString(newPass)
savePass(newPass)
Toast.makeText(requireActivity(),"Password updated!",Toast.LENGTH_SHORT).show()
findNavController().navigate(R.id.action_changePass_to_taskListFragment)
@ -91,20 +79,4 @@ class ChangePass : Fragment(){
}.apply()
}
private fun hashString(input: String): String {
return MessageDigest
.getInstance("SHA-256")
.digest(input.toByteArray())
.fold("", { str, it -> str + "%02x".format(it) })
}
private fun loadData(){
val sharedPreference =
requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE)
val savedString = sharedPreference.getString("STRING_KEY", null)
}
}

View File

@ -0,0 +1,12 @@
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) })
}
}

View File

@ -5,15 +5,11 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.tasklist.databinding.FragmentLoginPageBinding
import android.content.SharedPreferences
import android.widget.Toast
import java.lang.Exception
import java.math.BigInteger
import java.security.MessageDigest
import java.util.*
// TODO: Rename parameter arguments, choose names that match
@ -54,7 +50,8 @@ class LoginPage : Fragment(){
val sharedPreference =
requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE)
val insertedText = hashString(binding.password.text.toString())
val hash = HashString()
val insertedText = hash.hashString(binding.password.text.toString())
if(insertedText.equals(sharedPreference.getString("STRING_KEY", null),false)){
@ -75,11 +72,4 @@ class LoginPage : Fragment(){
}
}
private fun hashString(input: String): String {
return MessageDigest
.getInstance("SHA-256")
.digest(input.toByteArray())
.fold("", { str, it -> str + "%02x".format(it) })
}
}

View File

@ -10,6 +10,8 @@ import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.navigation.fragment.findNavController
import com.example.tasklist.databinding.FragmentSetPassBinding
import java.io.ByteArrayOutputStream
import java.io.ObjectOutputStream
import java.lang.Exception
import java.math.BigInteger
import java.security.MessageDigest
@ -59,7 +61,9 @@ class SetPass : Fragment() {
private fun savePass(pass: String) {
val password = hashString(pass)
val hash = HashString()
val password = hash.hashString(pass)
val sharedPreference =
requireActivity().getSharedPreferences("sharedPref", Context.MODE_PRIVATE)
@ -69,11 +73,4 @@ class SetPass : Fragment() {
}.apply()
}
private fun hashString(input: String): String {
return MessageDigest
.getInstance("SHA-256")
.digest(input.toByteArray())
.fold("", { str, it -> str + "%02x".format(it) })
}
}

View File

@ -8,35 +8,13 @@ import kotlin.collections.ArrayList
object DataSource {
private var listOfTasks: Array<String> = arrayOf("Zrobic zakupy",
"Zrobic zadanie domowe", "Pojsc na spacer",
"Posprzataс w domu", "Pomoc bratu", "Pospacerowac z psem", "Odebrac brata ze szkoly",
"Kupic laptop", "Zadzwonic do przyjaciol", "Zaspiewac piosenke")
private var _tasks: ArrayList<Task>? = null
val tasks: ArrayList<Task>
@RequiresApi(Build.VERSION_CODES.O)
get() {
if(_tasks == null) {
MockTasks()
}
return _tasks!!
}
var zadanie = ArrayList<Task>()
@RequiresApi(Build.VERSION_CODES.O)
private fun MockTasks() {
var pattern = "dd-MM-yyyy hh:mm"
var simpleDateFormat = SimpleDateFormat(pattern, Locale("pl", "PL"))
_tasks = ArrayList()
repeat((6..10).random()) {
indexTask ->
val newTask = Task(
listOfTasks[indexTask],
simpleDateFormat.format(Date())
)
_tasks?.add(newTask)
zadanie.add(newTask)
}
}
}

View File

@ -1,6 +1,6 @@
package com.example.tasklist.data
data class Task (
data class Task(
var name: String,
var date: String
)

View File

@ -0,0 +1,13 @@
package com.example.tasklist
import android.os.Build
import androidx.annotation.RequiresApi
class itemOnDis {
@RequiresApi(Build.VERSION_CODES.O)
fun encryptedToStr(str:String):String{
val crypting = ChCrypto
val taskCrypred = crypting.aesDecrypt(str)
return taskCrypred
}
}

View File

@ -10,6 +10,7 @@ import com.example.tasklist.R
import com.example.tasklist.data.Task
import com.example.tasklist.databinding.ItemTaskBinding
class TaskAdapter internal constructor(
private val nListener : TaskItemClickListener
): ListAdapter<Task, TaskAdapter.TaskViewHolder>(TaskDiffCallback()) {
@ -53,7 +54,7 @@ interface TaskItemClickListener {
private class TaskDiffCallback : DiffUtil.ItemCallback<Task> () {
override fun areItemsTheSame(oldItem: Task, newItem: Task): Boolean {
return oldItem.name == newItem.name
return oldItem.name.contentEquals(newItem.name)
}
override fun areContentsTheSame(oldItem: Task, newItem: Task): Boolean {

View File

@ -1,5 +1,6 @@
package com.example.tasklist.ui.choose.list
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -49,6 +50,7 @@ class TaskListFragment : Fragment(), TaskItemClickListener {
return binding.root
}
@SuppressLint("NotifyDataSetChanged")
private fun subscribeUi(newAdapter: TaskAdapter, binding: FragmentTaskListBinding) {
binding.recyclerView.adapter = newAdapter
newAdapter.notifyDataSetChanged()

View File

@ -18,8 +18,8 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="387dp"
android:layout_height="757dp"
android:layout_width="384dp"
android:layout_height="618dp"
android:layout_marginTop="64dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"

View File

@ -10,6 +10,11 @@
<variable
name="task"
type="com.example.tasklist.data.Task" />
<variable
name="crypto"
type="com.example.tasklist.ChCrypto" />
</data>