From 7d89a8d409ecb52ffe61778e42de8a69bb9d916a Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 28 Aug 2021 13:47:32 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=90=20Store=20credentials?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mylloon/mobidl/Credentials.kt | 44 ++++++++- .../java/com/mylloon/mobidl/MainActivity.kt | 98 +++++++++++++------ 2 files changed, 109 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/mylloon/mobidl/Credentials.kt b/app/src/main/java/com/mylloon/mobidl/Credentials.kt index 85b2506..c0002be 100644 --- a/app/src/main/java/com/mylloon/mobidl/Credentials.kt +++ b/app/src/main/java/com/mylloon/mobidl/Credentials.kt @@ -1,11 +1,13 @@ package com.mylloon.mobidl +import android.content.Context.MODE_PRIVATE import android.os.Build import android.security.keystore.KeyProperties import javax.crypto.Cipher import android.security.keystore.KeyGenParameterSpec import androidx.annotation.RequiresApi import java.security.* +import java.util.* class Credentials { @@ -13,6 +15,7 @@ class Credentials { private val provider = "AndroidKeyStore" // location of the key private val aliasKeyStore = "MobiDL" // name of the key private val transformation = "RSA/ECB/PKCS1Padding" // 'type' of the key + private val sharedPref = "com.mylloon.MobiDL" // shared pref name @RequiresApi(Build.VERSION_CODES.M) fun generateKey() { // generate RSA Keys @@ -56,4 +59,43 @@ class Credentials { val decryptedBytes = cipher.doFinal(message) return decryptedBytes.decodeToString() } -} \ No newline at end of file + + fun store(user: String, password: String) { // Store combo encrypted user/pass in shared preferences + val userEncrypted = encrypt(user) // encrypt user + val passwordEncrypted = encrypt(password) // encrypt password + val context = MainActivity.applicationContext() // get app context + + // show key : Base64.getEncoder().encodeToString(encryptedData) + + // write encrypted data to the app preference + val sharedPref = context.getSharedPreferences(sharedPref, MODE_PRIVATE) + with(sharedPref?.edit()) { + this?.putString("username", userEncrypted.contentToString()) + this?.putString("password", passwordEncrypted.contentToString()) + this?.apply() + } + } + + fun get(type: Int): String? { // type 0 -> username | type 1 -> password [ return null if there is nothing + val context = MainActivity.applicationContext() // get app context + + // read encrypted data from the app preference + val sharedPref = context.getSharedPreferences(sharedPref, MODE_PRIVATE) + var data: String? = null + when (type) { + 0 -> { data = sharedPref.getString("username", null) } + 1 -> { data = sharedPref.getString("password", null) } + } + + return if (data != null) { + val split: List = + data.substring(1, data.length - 1).split(", ") + val array = ByteArray(split.size) + for (i in split.indices) { + array[i] = split[i].toByte() + } + decrypt(array) + } else null + + } +} diff --git a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt index 58d569b..6631ac8 100644 --- a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt +++ b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt @@ -2,6 +2,7 @@ package com.mylloon.mobidl import android.Manifest +import android.content.Context import android.content.pm.PackageManager import android.os.Build import androidx.appcompat.app.AppCompatActivity @@ -24,6 +25,18 @@ class MainActivity : AppCompatActivity() { private var inSettings: Boolean = false // by default your not in settings page var prefs: SharedPreferences? = null // first run detection + init { + instance = this + } + + companion object { + private var instance: MainActivity? = null + + fun applicationContext() : Context { + return instance!!.applicationContext + } + } + private fun toggleSettingsButtonVisibility() { // Change Settings button visibility val visibility: Boolean = settingsButton?.findItem(R.id.settingsButton)?.isVisible == true settingsButton?.findItem(R.id.settingsButton)?.isVisible = !visibility @@ -32,39 +45,61 @@ class MainActivity : AppCompatActivity() { @RequiresApi(Build.VERSION_CODES.O) override fun onCreate(savedInstanceState: Bundle?) { // Main function super.onCreate(savedInstanceState) - setContentView(R.layout.activity_login) // show login page - prefs = getSharedPreferences("com.mylloon.MobiDL", MODE_PRIVATE) - val userInput = findViewById(R.id.usernameInput) - val passwordInput = findViewById(R.id.passwordInput) - val buttonInput = findViewById