New comments

This commit is contained in:
Mylloon 2021-08-28 01:56:10 +02:00
parent 34300daf3b
commit e53d501af3

View file

@ -10,12 +10,12 @@ import java.security.*
class Credentials {
private val provider = "AndroidKeyStore"
private val aliasKeyStore = "MobiDL"
private val transformation = "RSA/ECB/PKCS1Padding"
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
@RequiresApi(Build.VERSION_CODES.M)
fun generateKey() {
fun generateKey() { // generate RSA Keys
val keyPairGenerator = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_RSA, provider
)
@ -30,26 +30,26 @@ class Credentials {
keyPairGenerator.generateKeyPair()
}
private fun getPublicKey(): PublicKey {
private fun getPublicKey(): PublicKey { // Get the Public RSA key from the Android KeyStore
val keyStore: KeyStore = KeyStore.getInstance(provider)
keyStore.load(null)
return keyStore.getCertificate(aliasKeyStore).publicKey
}
private fun getPrivateKey(): PrivateKey {
private fun getPrivateKey(): PrivateKey { // Get the Private RSA key from the Android KeyStore
val keyStore: KeyStore = KeyStore.getInstance(provider)
keyStore.load(null)
return keyStore.getKey(aliasKeyStore, null) as PrivateKey
}
private fun encrypt(message: String): ByteArray {
private fun encrypt(message: String): ByteArray { // Encrypt a string with the public RSA key
val cipher = Cipher.getInstance(transformation)
cipher.init(Cipher.ENCRYPT_MODE, getPublicKey())
return cipher.doFinal(message.toByteArray())
}
private fun decrypt(message: ByteArray): String {
private fun decrypt(message: ByteArray): String { // Decrypt an encrypted string with the private RSA key
val cipher = Cipher.getInstance(transformation)
cipher.init(Cipher.DECRYPT_MODE, getPrivateKey())