Skip to content

Instantly share code, notes, and snippets.

@ksharma-xyz
Last active May 24, 2024 11:16
Show Gist options
  • Save ksharma-xyz/e96fa74b965da03f6a8ceebd3d53989b to your computer and use it in GitHub Desktop.
Save ksharma-xyz/e96fa74b965da03f6a8ceebd3d53989b to your computer and use it in GitHub Desktop.
Encryption and Decryption using Google Tink and managing keys using AndroidKeystore
import androidx.security.crypto.MasterKeys
import com.google.crypto.tink.integration.android.AndroidKeystoreAesGcm
object TinkCryptoManager {
// Get / Create Keys using Android Keystore
private val aesKeystore =
AndroidKeystoreAesGcm(MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC))
fun encrypt(plainText: String): ByteArray =
aesKeystore.encrypt(plainText.toByteArray(), "Data".toByteArray())
fun decrypt(cipherText: ByteArray): String {
val plaintTextBytes =
aesKeystore.decrypt(cipherText, "Data".toByteArray())
return String(plaintTextBytes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment