Adding Credential Support (RSA)
This commit is contained in:
parent
3960400036
commit
ac820e4c47
2 changed files with 77 additions and 0 deletions
59
app/src/main/java/com/mylloon/mobidl/Credentials.kt
Normal file
59
app/src/main/java/com/mylloon/mobidl/Credentials.kt
Normal file
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package com.mylloon.mobidl
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
|
import android.os.Build
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
|
@ -13,22 +14,28 @@ import android.text.TextWatcher
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
private var settingsButton: Menu? = null // before starting the app there is no settings button
|
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
|
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
|
private fun toggleSettingsButtonVisibility() { // Change Settings button visibility
|
||||||
val visibility: Boolean = settingsButton?.findItem(R.id.settingsButton)?.isVisible == true
|
val visibility: Boolean = settingsButton?.findItem(R.id.settingsButton)?.isVisible == true
|
||||||
settingsButton?.findItem(R.id.settingsButton)?.isVisible = !visibility
|
settingsButton?.findItem(R.id.settingsButton)?.isVisible = !visibility
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
override fun onCreate(savedInstanceState: Bundle?) { // Main function
|
override fun onCreate(savedInstanceState: Bundle?) { // Main function
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_login) // show login page
|
setContentView(R.layout.activity_login) // show login page
|
||||||
|
|
||||||
|
prefs = getSharedPreferences("com.mylloon.MobiDL", MODE_PRIVATE)
|
||||||
|
|
||||||
val userInput = findViewById<EditText>(R.id.usernameInput)
|
val userInput = findViewById<EditText>(R.id.usernameInput)
|
||||||
val passwordInput = findViewById<EditText>(R.id.passwordInput)
|
val passwordInput = findViewById<EditText>(R.id.passwordInput)
|
||||||
val buttonInput = findViewById<Button>(R.id.loginButton)
|
val buttonInput = findViewById<Button>(R.id.loginButton)
|
||||||
|
@ -58,6 +65,17 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.M)
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
if (prefs!!.getBoolean("firstrun", true)) {
|
||||||
|
println("Generate RSA keys...")
|
||||||
|
Credentials().generateKey()
|
||||||
|
println("Done!")
|
||||||
|
prefs!!.edit().putBoolean("firstrun", false).commit() // first run done, now the next ones won't be "first".
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean { // add settings button
|
override fun onCreateOptionsMenu(menu: Menu?): Boolean { // add settings button
|
||||||
menuInflater.inflate(R.menu.menu_main, menu)
|
menuInflater.inflate(R.menu.menu_main, menu)
|
||||||
settingsButton = menu
|
settingsButton = menu
|
||||||
|
|
Reference in a new issue