From ac820e4c479ed3709c2104dc070cca4e25303f89 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 28 Aug 2021 01:44:48 +0200 Subject: [PATCH] Adding Credential Support (RSA) --- .../java/com/mylloon/mobidl/Credentials.kt | 59 +++++++++++++++++++ .../java/com/mylloon/mobidl/MainActivity.kt | 18 ++++++ 2 files changed, 77 insertions(+) create mode 100644 app/src/main/java/com/mylloon/mobidl/Credentials.kt diff --git a/app/src/main/java/com/mylloon/mobidl/Credentials.kt b/app/src/main/java/com/mylloon/mobidl/Credentials.kt new file mode 100644 index 0000000..9b1acb6 --- /dev/null +++ b/app/src/main/java/com/mylloon/mobidl/Credentials.kt @@ -0,0 +1,59 @@ +package com.mylloon.mobidl + +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.* + + +class Credentials { + + private val provider = "AndroidKeyStore" + private val aliasKeyStore = "MobiDL" + private val transformation = "RSA/ECB/PKCS1Padding" + + @RequiresApi(Build.VERSION_CODES.M) + fun generateKey() { + val keyPairGenerator = KeyPairGenerator.getInstance( + KeyProperties.KEY_ALGORITHM_RSA, provider + ) + keyPairGenerator.initialize( + KeyGenParameterSpec.Builder( + aliasKeyStore, + KeyProperties.PURPOSE_DECRYPT + ) + .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1) + .build() + ) + keyPairGenerator.generateKeyPair() + } + + private fun getPublicKey(): PublicKey { + val keyStore: KeyStore = KeyStore.getInstance(provider) + keyStore.load(null) + return keyStore.getCertificate(aliasKeyStore).publicKey + } + + private fun getPrivateKey(): PrivateKey { + val keyStore: KeyStore = KeyStore.getInstance(provider) + keyStore.load(null) + return keyStore.getKey(aliasKeyStore, null) as PrivateKey + } + + private fun encrypt(message: String): ByteArray { + val cipher = Cipher.getInstance(transformation) + cipher.init(Cipher.ENCRYPT_MODE, getPublicKey()) + + return cipher.doFinal(message.toByteArray()) + } + + private fun decrypt(message: ByteArray): String { + val cipher = Cipher.getInstance(transformation) + cipher.init(Cipher.DECRYPT_MODE, getPrivateKey()) + + val decryptedBytes = cipher.doFinal(message) + return decryptedBytes.decodeToString() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt index 3b1d47d..58d569b 100644 --- a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt +++ b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt @@ -3,6 +3,7 @@ package com.mylloon.mobidl import android.Manifest import android.content.pm.PackageManager +import android.os.Build import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button @@ -13,22 +14,28 @@ import android.text.TextWatcher import android.view.MenuItem import android.view.Menu import android.widget.Toast +import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat +import android.content.SharedPreferences class MainActivity : AppCompatActivity() { private var settingsButton: Menu? = null // before starting the app there is no settings button private var inSettings: Boolean = false // by default your not in settings page + var prefs: SharedPreferences? = null // first run detection private fun toggleSettingsButtonVisibility() { // Change Settings button visibility val visibility: Boolean = settingsButton?.findItem(R.id.settingsButton)?.isVisible == true settingsButton?.findItem(R.id.settingsButton)?.isVisible = !visibility } + @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