security-crypto-ktx
- User
security-crypto-ktx
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
security-crypto-ktx
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Looks like you might be referring to Android Jetpack’s Security Crypto KTX library. Below is a quick primer in case you’re:
• hunting for the Maven/Gradle coordinates
• wondering what the KTX layer adds on top of the plain androidx.security:security-crypto library
• looking for code snippets (EncryptedSharedPreferences, EncryptedFile, etc.)
dependencies {
// Core Security Crypto
implementation "androidx.security:security-crypto:1.1.0-alpha06"
// Kotlin extensions (this is the one usually called “security-crypto-ktx”)
implementation "androidx.security:security-crypto-ktx:1.1.0-alpha06"
}
The KTX artifact is a very thin layer of Kotlin-friendly extensions over the core library, so you nearly always use both together.
Minimum SDK = 23 (Android 6.0).
MasterKey builder, etc.).use blocks).Under the hood you still rely on the same Tink-backed cryptography primitives.
// Build or retrieve a master key. KTX makes this a one-liner.
val masterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
val securePrefs = context.encryptedSharedPreferences(
"secure_prefs",
masterKey,
prefKeyEncryptionScheme = EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
prefValueEncryptionScheme = EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
// Write
securePrefs.edit {
putString("jwt", token)
putBoolean("isLoggedIn", true)
}
// Read
val token = securePrefs.getString("jwt", null)
KTX goodies here:
• context.encryptedSharedPreferences(…) (an extension)
• edit { … } from androidx.core.content.edit for concise edits
suspend fun saveSecureFile(context: Context, bytes: ByteArray) {
val masterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
val file = File(context.filesDir, "secret.pdf")
// The KTX version offers a suspend extension for I/O
context.encryptToFile(
file = file,
masterKey = masterKey,
byteArray = bytes,
fileEncryptionScheme = EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
)
}
suspend fun readSecureFile(context: Context): ByteArray {
val masterKey = MasterKey(context) // retrieves existing key
val file = File(context.filesDir, "secret.pdf")
return context.decryptFileToBytes(
file = file,
masterKey = masterKey,
fileEncryptionScheme = EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
)
}
No manual streams, no withContext(Dispatchers.IO) { … }; the KTX suspend helpers handle threading internally.
| Concern | Tip |
|---|---|
| Backup & restore | If you rely on Android auto-backup, remember that MasterKey is device-specific. On restore to a new device, the key is gone → decryption fails. Consider disabling backup for files encrypted with device-bound keys, or handle migration gracefully. |
| Biometric gating | Combine with BiometricPrompt and StrongBox via setUserAuthenticationRequired(true) on MasterKey.Builder if you need auth-gated keys. |
| Performance | AES-GCM is fast on most modern devices, but encrypt only what truly needs it. |
| Testing | Robolectric 4.9+ has basic support but sometimes you’ll need device/emulator tests for crypto. |
• Official docs: https://developer.android.com/topic/security/data
• Source code (KTX module): https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:security/security-crypto-ktx/
• Tink library (under the hood): https://github.com/google/tink
Need something more specific (e.g., key rotation, migrating from SQLCipher, CommonCrypto interop)? Let me know!