Skip to content

Latest commit

Β 

History

History
99 lines (83 loc) Β· 5.39 KB

File metadata and controls

99 lines (83 loc) Β· 5.39 KB

Migrating from Encrypted SharedPreferences to Jetpack DataStore

Kotlin Android Encryption

This repository contains the code accompanying the article
Migrating from Encrypted SharedPreferences to Jetpack DataStore.
It showcases a step-by-step transition from Encrypted SharedPreferences to Jetpack DataStore.

πŸ“Œ About the Project

Migrating from Encrypted SharedPreferences to Jetpack DataStore improves data security and performance in Android apps.
This repository demonstrates each migration step in different branches:

  1. branch-1-initial-setup – Initial implementation using EncryptedSharedPreferences.
  2. branch-2-introduce-datastore – Introduced DataStore without migration.
  3. branch-3-migration-setup – Set up migration logic from EncryptedSharedPreferences to DataStore.
  4. branch-4-migration-implementation – Implemented migration logic.
  5. branch-5-final-version – Final version with a complete migration process.

πŸš€ Final Version (branch branch-5-final-version)

The final version includes:

  • βœ… Fully implemented migration from EncryptedSharedPreferences to Jetpack DataStore.
  • βœ… Secure encryption using AES-GCM.
  • βœ… Proper key management with MasterKey and a keyset similar to Tink library.

πŸ”Ή How to Run the Migration Process

  1. Initial Setup

    • Open AppComponent.kt and set the preferences provider to SharedPreferencesProvider:
      mainActivity.preferences = sharedPreferencesProvider
    • Run the application and enter some data to be stored.
  2. Switch to DataStore with Migration

    • Open AppComponent.kt and replace the preferences provider with DataStorePreferencesProvider:
      mainActivity.preferences = dataStorePreferencesProvider
    • Run the application again. The stored data will be migrated to Jetpack DataStore.
  3. Verify Migration Success

    • Check the logs using the tag "TAG11". The logs will show which data has been successfully migrated to DataStore.

πŸ” Security Details (branch branch-5-final-version)

The final version ensures secure data storage by implementing:

  • AES-GCM encryption for securing stored values.
  • MasterKey creation for key management, similar to how Tink library handles encryption.
  • AES-256 encryption for key protection, ensuring that encryption keys remain secure.
  • Encrypted keyset storage, making sure keys are safely managed and protected.

πŸ”Ή Key & Value Encryption Details

  • Keys encryption (AES-256-SIV-CMAC):

    • Keys are encrypted using AES-256-SIV-CMAC, where the IV (initialization vector) is deterministically computed.
    • This means that for the same input, the ciphertext will always be the same.
    • The underlying mode is AES/CTR/NoPadding, a stream cipher mode without built-in integrity checks.
  • Values encryption (AES-256-GCM):

    • Values are encrypted using AES-256-GCM, where the IV is randomly generated for each encryption operation.
    • This ensures that even for the same input, the ciphertext will always be different.
    • The underlying mode is AES/GCM/NoPadding, which includes built-in authentication (tag) to prevent data tampering.

πŸ“‚ Project Structure

πŸ“‚ app
 β”œβ”€β”€ πŸ“‚ data
 β”‚    β”œβ”€β”€ SharedPrefsManager.kt  // Handles EncryptedSharedPreferences
 β”‚    β”œβ”€β”€ DataStoreManager.kt    // Handles Jetpack DataStore
 β”‚    β”œβ”€β”€ DataMigration.kt       // Migration logic
 β”‚    └── DataModel.proto        // Proto DataStore schema
 β”œβ”€β”€ πŸ“‚ ui
 β”‚    β”œβ”€β”€ MainActivity.kt        // Demonstrates data usage
 β”‚    └── SettingsFragment.kt    // Settings screen using DataStore

πŸ”§ Installation & Running

  1. Clone the repository:
    git clone https://github.com/KyrBabenko/encrypted-data-store-migration.git
  2. Checkout the final branch:
    git checkout branch-5-final-version
  3. Open the project in Android Studio and run it.

πŸ› οΈ Technologies Used

  • Kotlin
  • Jetpack DataStore (Proto DataStore)
  • EncryptedSharedPreferences
  • AES-256-SIV-CMAC for key encryption
  • AES-256-GCM for value encryption
  • Coroutines & Flow

πŸ“– Read the Article

A detailed migration guide is available in the Medium article, Linkedin article.

πŸ“ License

This project is released under an open-source license with no restrictions.