Last active
May 24, 2024 11:16
-
-
Save ksharma-xyz/e96fa74b965da03f6a8ceebd3d53989b to your computer and use it in GitHub Desktop.
Encryption and Decryption using Google Tink and managing keys using AndroidKeystore
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
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