Iced Tea Labs Share to be shared

Android Data Encryption using Jetpack Security

Android Data Encryption using Jetpack Security

Photo by [Philipp Katzenberger]( on [Unsplash](

As an Android developer, you probably want to encrypt your application data at some points for security reasons. Those sensitive data can be varied from personal identifiable information (PII), financial records, to enterprise-related data.

By using Jetpack Security (JetSec), you can easily encrypt Files and SharePreferences locally to protect your sensitive information.

Include JetSec into the project

To use JetSec in your project, just simply adding the dependencies below in your app’s build.gradle:

dependencies {
    implementation ""

Key Generation

JetSec library uses the 2-part system for key management:

Before doing any cryptographic operation, you have to generate the key first.

Out of the box, JetSec provides the default master key within the MasterKey class with the AES256_GCM_SPEC specification. The master key is generated and stored in the Android Keystore system, which makes it difficult to extract the key material.

val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.*AES256_GCM_SPEC*)

The default master key should be fine for general purposes. In case you want to create a master key with a custom specification, JetSpec also supports that:

val customSpec = KeyGenParameterSpec.Builder(
        KeyProperties.*PURPOSE_ENCRYPT *or KeyProperties.*PURPOSE_DECRYPT
        .*apply ***{
            **if (Build.VERSION.*SDK_INT *>= Build.VERSION_CODES.*P*) {

val masterKeyAlias = MasterKeys.getOrCreate(customSpec)


If you need to save data in the key-value format securely, JetSpec provides EncryptedSharedPreferences which share the same interface with the normal SharedPreferences.

Using EncryptedSharedPreferences, both keys and values are encrypted:

The below code snippets show you how to edit a record of the encrypted shared preferences:

val sharedPrefs: SharedPreferences = EncryptedSharedPreferences.create(

        .putString("auth_token", "random_auth_token")

And the result 🎉


The EncryptedFile class provides custom implementation of FileInputStream and FileOutputStream, allows you to read and write encrypted files easier.

To provide secure read and write operations from file streams, JetSec uses the Streaming Authenticated Encryption with Associated Data (AEAD) primitive. The data is divided into chunks and encrypted using AES256-GCM.

The below code snippets show you how to write data into file securely:

val fileToWrite = File(getDir("sensitive_data", *MODE_PRIVATE*), "encrypted_data.txt")
val encryptedFile = EncryptedFile.Builder(

val fileContent = "Hello world!"

        .*run ***{

… and read encrypted data from a file securely:

val fileToRead = File(getDir("sensitive_data", *MODE_PRIVATE*), "encrypted_data.txt")
val encryptedFile = EncryptedFile.Builder(

val byteArrayOutputStream = ByteArrayOutputStream()
encryptedFile.openFileInput().*run ***{
    **var nextByte: Int = read()

    while (nextByte != -1) {
        nextByte = read()


**val plaintext = *String*(byteArrayOutputStream.toByteArray())

And the result 🎉

If you have any feedback/corrections, please feel free to leave a comment below.

Thanks for reading and happy coding! 💻

comments powered by Disqus