Encrypted Preferences with Google Tink: Navigating Android Data Encryption

Jay Patel
5 min readNov 18, 2023
Source: Google Tink

In today’s digital era, mobile applications handle a vast amount of sensitive user data, ranging from personal information to financial credentials. Ensuring the confidentiality and integrity of this data is paramount for maintaining user trust and preventing potential security breaches. Android, a widely used mobile operating system, provides various mechanisms for storing application data, including Shared Preferences. However, Shared Preferences, exposes sensitive data as it saves data in a simple text as key-value pairs. We will be learning more about how to prevent this in this blog.

Section Breakdown

  1. Understanding Preference: Shared Preferences vs Encrypted Preferences
  2. Google Tink: Features and Implementation
  3. Security Comparison: Risks and Benefits
  4. Performance Considerations: Speed and Efficiency
  5. Use Cases: Scenarios for Each Approach
  6. Conclusion
  7. FAQs

Understanding Preferences Shared Preferences vs Encrypted Preferences

The fundamental contrast lies in their data-handling methodologies. Shared Preferences involve storing primitive data types, and simplifying access for apps but lack encryption. Conversely, Google Tink offers a cryptographic toolkit enabling secure data storage, adding a layer of protection to sensitive information.

Shared Preferences

Functionality and Implementation

Shared Preferences, a default Android API, facilitate the storage of simple data types as key-value pairs in an XML file. This mechanism operates seamlessly, providing apps with an efficient means of storing and retrieving small chunks of data without the complexity of a full-scale database.

Shared Preferences with no encryption

You can see the saved location in Android Studio in the right corner, one can find an option as Device File Explorer> data > data folder> your package> shared_prefs. Inside the shared_prefs folder you can see the xml file as my_preferences.xml

my_preferences.xml with no encryption

Google Tink

Features and Implementation
Google Tink presents a robust encryption mechanism providing symmetric and asymmetric encryption tools. It ensures data confidentiality, integrity, and authenticity, adding a powerful security layer to Android apps.

To implement Encrypted Preferences with Google Tink, follow these steps:

  1. Add the Tink library to your project.
  2. Create a KeysetHandle.
  3. Create an AndroidKeysetManager.
  4. Create an EncryptedSharedPreferences instance.

This code defines a utility class called `EncryptedPreferences` that facilitates secure storage in Android `SharedPreferences`. It employs encryption to store data securely. Here’s a breakdown:

1. Builder Pattern: The class utilizes a builder pattern (Builder class) to configure the preferences instance. It allows setting the context, preference name, and whether to use default preferences.

2. Initialization: Upon building, it initializes the cryptographic settings (MasterKey, encryption schemes), sets up the EncryptedSharedPreferences, and prepares the cryptographic primitives (Aead).

3. Encryption/Decryption: It provides methods (encryptText and decryptText) to encrypt and decrypt text data using the cryptographic primitives.

4. Value Storage/Retrieval: Methods are available to store and retrieve different types of values (string, boolean, long, int, double, float) into/from the SharedPreferences. Before storing, it encrypts the value, and while retrieving, it decrypts the stored value.

5. Debug Mode Handling: There’s a provision to disable encryption for debug builds. In debug mode, it retrieves all values, including those marked for debugging (DEBUG_SUFFIX). In non-debug mode, it decrypts and retrieves only non-debug values.

6. Utility Functions: It provides functions to clear the SharedPreferences entirely (clear), remove a specific key-value pair (removeKey), and retrieve all key-value pairs (getAll).

7. Base64 Encoding/Decoding: The code uses Base64 encoding and decoding to transform byte arrays into strings and vice versa.

Overall, this EncryptedPreferences class serves as a secure wrapper around SharedPreferences, encrypting sensitive data before storage and decrypting it upon retrieval, ensuring data security in Android apps.

PreferenceManager class serves as a generic interface to handle various. preferences within an application, employing the provided EncryptedPreferences object.

App Module for Singleton

This code is part of an Android app module (AppModule) that utilizes Dagger Hilt for dependency injection.

After implementing Encrypted Preferences with Google tink

Security Comparison: Risks and Benefits

Comparatively, Shared Preferences offer simplicity but pose security risks, especially for sensitive data. Google Tink excels in data security but demands additional implementation efforts, striking a balance between convenience and robust protection.

Performance Considerations: Speed and Efficiency

While Shared Preferences prioritize efficiency, Google Tink’s encryption operations might affect performance. Balancing security and app responsiveness becomes crucial for optimal user experience.

Use Cases: Scenarios for Each Approach

Understanding the practical scenarios where each approach shines is crucial in making informed decisions about data storage and security in Android apps.

Shared Preferences:
User Settings:
Shared Preferences excel in storing user preferences such as language settings, theme choices, or user interface preferences that don’t involve critical or sensitive data.
App Configuration: For non-sensitive configurations like layout options or notification settings, Shared Preferences offer a convenient solution without the need for heavy encryption.

Basic Demo for saving App Configuration/ User Setting in Shared Preferences without encryption

Google Tink — Encrypted Preferences:
Sensitive Data Storage: Google Tink becomes indispensable when dealing with sensitive user information like passwords, authentication tokens, or financial data. It ensures robust encryption, safeguarding critical data from unauthorized access.
Compliance-Driven Apps: Applications that must adhere to stringent security standards, such as those handling healthcare or financial information, greatly benefit from Google Tink’s encryption capabilities.

By defining the suitable scenarios for each approach, developers can strategically leverage Shared Preferences for non-critical data and harness the robust security features of Google Tink for sensitive information, ensuring a balance between functionality, efficiency, and data protection in Android applications.

Conclusion

Navigating the choices between Shared Preferences and Google Tink underscores the pivotal balance between convenience, efficiency, and data security in Android app development.

FAQs

1. Is Shared Preferences entirely insecure?
Shared Preferences lack inherent encryption, making them vulnerable to data breaches if used for sensitive information storage.

2. Does Google Tink significantly impact app performance?
The impact of Google Tink on app performance isn’t significant. Encryption operations might cause a slight reduction in speed, but optimizing the implementation can minimize any notable drops in performance. Moreover, with advancements in mobile hardware, the impact on performance tends to be mitigated.

3. Are there other alternatives apart from Shared Preferences and Google Tink?
Yes, developers can explore alternatives like Android Keystore, CMake, or third-party libraries for varying encryption needs.

4. Can Shared Preferences be made more secure?
Supplementing Shared Preferences with additional encryption or obfuscation techniques can enhance security to a certain extent.

5. Which approach is more developer-friendly?
Shared Preferences offer simplicity, whereas Google Tink demands a learning curve but ensures robust security measures.

References

  1. Sample app in GitHub
  2. Android Security Guidelines
  3. Android Shared Preferences
  4. Google Tink
  5. Encrypted Shared Preferences

Please feel free to reach out to me on LinkedIn and Twitter

This comparison between Shared Preferences and Google Tink in Android aims to guide developers in making informed choices aligning with both security and performance objectives. Thanks for reading this. Happy Encryption! :)

--

--

Jay Patel

Software Engineer @QBurst || IITP '25 Cloud Computing