Added code for encrypting username and password during device registration
This commit is contained in:
@@ -19,8 +19,8 @@ android {
|
||||
applicationId "in.sminnovations.hpostesting.dev"
|
||||
minSdk 21
|
||||
targetSdk 34
|
||||
versionCode 113
|
||||
versionName "2.1.113"
|
||||
versionCode 114
|
||||
versionName "2.1.114"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.example.hpostesting.data.encryption
|
||||
|
||||
import android.security.keystore.KeyGenParameterSpec
|
||||
import android.security.keystore.KeyProperties
|
||||
import android.util.Base64
|
||||
import javax.crypto.Cipher
|
||||
import javax.crypto.KeyGenerator
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
|
||||
|
||||
object AESCrypt {
|
||||
private const val ALGORITHM = "AES"
|
||||
private const val KEY = "your_secret_key"
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun encrypt(value: String): String {
|
||||
val keySpec = SecretKeySpec(KEY.toByteArray(), ALGORITHM)
|
||||
val cipher = Cipher.getInstance(ALGORITHM)
|
||||
cipher.init(Cipher.ENCRYPT_MODE, keySpec)
|
||||
val encryptedBytes = cipher.doFinal(value.toByteArray())
|
||||
return Base64.encodeToString(encryptedBytes, Base64.DEFAULT)
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun decrypt(encrypted: String?): String {
|
||||
val keySpec = SecretKeySpec(KEY.toByteArray(), ALGORITHM)
|
||||
val cipher = Cipher.getInstance(ALGORITHM)
|
||||
cipher.init(Cipher.DECRYPT_MODE, keySpec)
|
||||
val encryptedBytes = Base64.decode(encrypted, Base64.DEFAULT)
|
||||
val decryptedBytes = cipher.doFinal(encryptedBytes)
|
||||
return String(decryptedBytes)
|
||||
}
|
||||
|
||||
//generates a secret key for the encryption functions to use
|
||||
//TODO: Generating keys requires a higher API level. ask someone if this is okay.
|
||||
}
|
||||
@@ -93,7 +93,6 @@ class DeviceProvisionActivity : AppCompatActivity(), DeviceCommunicationHandler
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
setupListener()
|
||||
connectUsb(false)
|
||||
|
||||
}
|
||||
|
||||
private fun setupListener() {
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
import android.util.Base64
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
@@ -15,6 +16,7 @@ import androidx.lifecycle.MutableLiveData
|
||||
import com.example.hpostesting.data.Result
|
||||
import com.example.hpostesting.data.constant.Constants
|
||||
import com.example.hpostesting.data.constant.HemoCubeCommands
|
||||
import com.example.hpostesting.data.encryption.AESCrypt
|
||||
import com.example.hpostesting.data.model.deviceprovision.DeviceProvisionRequest
|
||||
import com.example.hpostesting.data.model.patient.DeviceData
|
||||
import com.example.hpostesting.presentation.UsbServiceListener
|
||||
@@ -22,6 +24,9 @@ import com.example.hpostesting.presentation.dashboard.DashboardActivity
|
||||
import com.google.firebase.crashlytics.ktx.crashlytics
|
||||
import com.google.firebase.ktx.Firebase
|
||||
import `in`.sminnovations.hpostesting.databinding.FragmentDeviceProvisionBinding
|
||||
import javax.crypto.Cipher
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
|
||||
|
||||
class DeviceProvisionFragment : Fragment() {
|
||||
private var resultData: String = ""
|
||||
@@ -123,6 +128,9 @@ class DeviceProvisionFragment : Fragment() {
|
||||
Log.e("idpass", response.toString())
|
||||
Log.e("idpass", response.data.data?.credentials?.username.toString())
|
||||
Log.e("idpass", deviceProvisionResponse)
|
||||
|
||||
saveDataToLocalFile(response.data.data?.credentials?.username.toString(), response.data.data?.credentials?.password.toString())
|
||||
|
||||
} else {
|
||||
Toast.makeText(
|
||||
activity,
|
||||
@@ -218,4 +226,17 @@ class DeviceProvisionFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//saves username and password to a local file after encrypting it.
|
||||
private fun saveDataToLocalFile(username : String, password : String){
|
||||
val encryptedUsername = AESCrypt.encrypt(username)
|
||||
val encryptedPassword = AESCrypt.encrypt(password)
|
||||
val encryptedData = encryptedUsername + "\n" + encryptedPassword
|
||||
|
||||
val fos = requireContext().openFileOutput("credentials.txt", Context.MODE_PRIVATE)
|
||||
fos.write(encryptedData.toByteArray())
|
||||
fos.close()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user