This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private var deviceSecurityAlert: AlertDialog? = null | |
override fun onStart() { | |
super.onStart() | |
if (!systemServices.isDeviceSecure()) { | |
deviceSecurityAlert = systemServices.showDeviceSecurityAlert() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val keyStore: KeyStore = createAndroidKeyStore() | |
private fun createAndroidKeyStore(): KeyStore { | |
val keyStore = KeyStore.getInstance("AndroidKeyStore") | |
keyStore.load(null) | |
return keyStore | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@TargetApi(Build.VERSION_CODES.M) | |
private fun initGeneratorWithKeyGenParameterSpec(generator: KeyPairGenerator, alias: String) { | |
val builder = KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT) | |
.setBlockModes(KeyProperties.BLOCK_MODE_ECB) | |
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1) | |
generator.initialize(builder.build()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun initGeneratorWithKeyPairGeneratorSpec(generator: KeyPairGenerator, alias: String) { | |
val startDate = Calendar.getInstance() | |
val endDate = Calendar.getInstance() | |
endDate.add(Calendar.YEAR, 20) | |
val builder = KeyPairGeneratorSpec.Builder(context) | |
.setAlias(alias) | |
.setSerialNumber(BigInteger.ONE) | |
.setSubject(X500Principal("CN=${alias} CA Certificate")) | |
.setStartDate(startDate.time) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getAndroidKeyStoreAsymmetricKeyPair(alias: String): KeyPair? { | |
val privateKey = keyStore.getKey(alias, null) as PrivateKey? | |
val publicKey = keyStore.getCertificate(alias)?.publicKey | |
return if (privateKey != null && publicKey != null) { | |
KeyPair(publicKey, privateKey) | |
} else { | |
null | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
companion object { | |
var TRANSFORMATION_ASYMMETRIC = "RSA/ECB/PKCS1Padding" | |
} | |
val cipher: Cipher = Cipher.getInstance(transformation) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun encrypt(data: String, key: Key?): String { | |
cipher.init(Cipher.ENCRYPT_MODE, key) | |
val bytes = cipher.doFinal(data.toByteArray()) | |
return Base64.encodeToString(bytes, Base64.DEFAULT) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun decrypt(data: String, key: Key?): String { | |
cipher.init(Cipher.DECRYPT_MODE, key) | |
val encryptedData = Base64.decode(data, Base64.DEFAULT) | |
val decodedData = cipher.doFinal(encryptedData) | |
return String(decodedData) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun createAndroidKeyStoreAsymmetricKey(alias: String): KeyPair { | |
val generator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore") | |
if (SystemServices.hasMarshmallow()) { | |
initGeneratorWithKeyGenParameterSpec(generator, alias) | |
} else { | |
initGeneratorWithKeyPairGeneratorSpec(generator, alias) | |
} | |
// Generates Key with given spec and saves it to the KeyStore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.setCertificateNotBefore(startDate) // By default, this date is Jan 1 1970. | |
.setCertificateNotAfter(endDate) // By default, this date is Jan 1 2048. | |
.setCertificateSerialNumber(number) // By default, the serial number is 1. | |
.setCertificateSubject(x500Principal) // By default, the subject is CN=fake. |