-
-
Save faiz786/6c0c2167b36bebf067506c0c83d9b026 to your computer and use it in GitHub Desktop.
Android Encryptor (Helper class)
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
class Encryptor(private val sharedPreferences: SharedPreferences) { | |
private val ANDROID_KEY_STORE = "AndroidKeyStore" | |
private var initVector: ByteArray? = null | |
private var encryption: ByteArray? = null | |
private val TRANSFORMATION = "AES/GCM/NoPadding" | |
@Throws(Exception::class) | |
fun encryptTextWithAES(alias: String, textToEncrypt: String): ByteArray? { | |
var alias = alias | |
alias = alias + "_AES" | |
val cipher = Cipher.getInstance(TRANSFORMATION) | |
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(alias, KeyProperties.KEY_ALGORITHM_AES)) | |
initVector = cipher.iv | |
encryption = cipher.doFinal(textToEncrypt.toByteArray(StandardCharsets.UTF_8)) | |
val base64InitVector = Base64.encodeToString(initVector, Base64.NO_WRAP) | |
val base64Encryption = Base64.encodeToString(encryption, Base64.NO_WRAP) | |
sharedPreferences.edit().putString(alias + "_initVector", base64InitVector).apply() | |
sharedPreferences.edit().putString(alias + "_encryption", base64Encryption).apply() | |
Log.d("AndroidKeyStore", "aliases: " + getAllAliasesInTheKeystore()) | |
return encryption | |
} | |
@Throws(Exception::class) | |
private fun getAllAliasesInTheKeystore(): ArrayList<String> { | |
val keyStore = KeyStore.getInstance("AndroidKeyStore") | |
keyStore.load(null) | |
return Collections.list(keyStore.aliases()) | |
} | |
@Throws(Exception::class) | |
private fun getSecretKey(alias: String, algorithm: String): SecretKey { | |
val keyGenerator: KeyGenerator | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
keyGenerator = KeyGenerator.getInstance(algorithm, ANDROID_KEY_STORE) | |
keyGenerator.init(KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT) | |
.setBlockModes(KeyProperties.BLOCK_MODE_GCM) | |
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) | |
.build()) | |
} else { | |
keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE) | |
keyGenerator.init(SecureRandom.getInstance(KeyProperties.KEY_ALGORITHM_AES)) | |
} | |
return keyGenerator.generateKey() | |
} | |
fun encryptTextWithRSA(alias: String, textToEncrypt: String): ByteArray? { | |
var alias = alias | |
alias = alias + "_RSA" | |
var publicKey: Key? = null | |
var privateKey: Key? = null | |
try { | |
getAllAliasesInTheKeystore() | |
val kpg = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, ANDROID_KEY_STORE) | |
kpg.initialize(1024) | |
val kp = kpg.genKeyPair() | |
publicKey = kp.public | |
privateKey = kp.private | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
// Encode the original data with RSA private key | |
var encodedBytes: ByteArray? = null | |
try { | |
val c = Cipher.getInstance("RSA") | |
c.init(Cipher.ENCRYPT_MODE, privateKey) | |
encodedBytes = c.doFinal(textToEncrypt.toByteArray()) | |
val base64PublicKey = Base64.encodeToString(publicKey!!.encoded, Base64.NO_WRAP) | |
val base64Encrypted = Base64.encodeToString(encodedBytes, Base64.NO_WRAP) | |
sharedPreferences.edit().putString(alias + "_publicKey", base64PublicKey).apply() | |
sharedPreferences.edit().putString(alias + "_encryption", base64Encrypted).apply() | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
return encodedBytes | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment