Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

secure-storage-android

> 编程语言
Open source

Store strings & credentials securely encrypted on your device

375 stars0 likes0 views
WebsiteGitHub

About

Store strings & credentials securely encrypted on your device

Secure Device Storage - Android

Storing Credentials Securely on Android Devices

Introduction

Storing credentials securely on a device is in many occasions necessary. You probably don't want to rely only on the separation of processes of the Android OS but make sure the stored values are also encrypted. To make that possible we have combined the Android Keystore and the SharedPreferences. The keystore is used for generating cryptographic keys, the values are then encrypted with these keys and subsequently securely stored in the SharedPreferences.

The secure part about this solution is that those generated keys are never exposed to the kernel when the device is equipped with a “Trusted Execution Environment”. A so called TEE is a secure area inside the main processor of a smartphone which runs code isolated from other processes. That means even if the device gets compromised or hacked those keys can’t be extracted. Already a lot of modern Android phones out there are equipped with a TEE (mostly because it’s often used to play DRM protected material) and it even is a requirement for Google’s Android Nougat certification — so every phone running Android Nougat and later will come with a TEE installed.

SecureStorage uses its own dedicated private SharedPreferences to prevent conflicts with other possible SharedPreference instances and ensure that the content of the SecureStorage can only be accessed from the app which uses this library.

Supported API's

Symmetric key generation and storage in the Android KeyStore is supported from Android 6.0 (API Level 23) onwards. Asymmetric key generation and storage in the Android KeyStore is supported from Android 4.3 (API Level 18) onwards.

To support more devices SecureStorage uses for now the asymmetric key generation, which in the case of storing simple credentials is very secure and the potential lack of speed in contrast to symmetric key generation, is not noticeable. Nevertheless, make sure to move the execution into a background thread as encryption does take a little time.

Usage

Add the library to your apps build.gradle:

groovy
implementation "de.adorsys.android:securestoragelibrary:${latestSecureStorageVersion}"

To store a string value in your SecureStorage you have to call:

kotlin
SecurePreferences.setValue(context, "KEY", "PLAIN_MESSAGE")

This works for every other primitive data type. So for storing a boolean value:

kotlin
SecurePreferences.setValue(context, "KEY", true/false)

for int

kotlin
SecurePreferences.setValue(context, "KEY", 100)

for float and long

kotlin
SecurePreferences.setValue(context, "KEY", 100.12345)

To retrieve a string value:

kotlin
SecurePreferences.getStringValue(context, "KEY", ""/null)

And respectively for the other types

kotlin
SecurePreferences.getBooleanValue(context, "KEY", false/true)
kotlin
SecurePreferences.getIntValue(context, "KEY", 0)
kotlin
SecurePreferences.getFloatValue(context, "KEY", 0F)
kotlin
SecurePreferences.getLongValue(context, "KEY", 0L)

See if an entry exists in the SecurePreferences. Also returns false if the key pair does not exist:

kotlin
SecurePreferences.contains(context, "KEY")

You can also remove an entry from the SecurePreferences:

kotlin
SecurePreferences.removeValue(context, "KEY")

Clearing the SecurePreferences and deleting the KeyPair:

kotlin
SecurePreferences.clearAllValues(context)

Everything about the cryptographic keys such as generating, maintaining and usage is handled internally by the module, so you do not need to worry about it.

If you want to keep track of changes in your SecureStorage you can register an OnSharedPreferencesChangeListener as follows:

kotlin
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
    // check if the key is the one you are listening for and react
}
SecurePreferences.registerOnSharedPreferenceChangeListener(this, listener)

Unregister the listener as soon as you don't need it any more with

kotlin
SecurePreferences.unregisterOnSharedPreferenceChangeListener(context, listener)

Error handling

The library throws for everything a SecureStorageException. Within the SecureStorageException you can find a exception type. You can handle the error which occurred with the help of this type as follows:

…

Contributors:

@drilonrecica

@luckyhandler

Want to know more:

These links cover security aspect of the android keystore:

This link covers security aspect of the android storage:

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Javaandroidandroid-keystoreandroid-storageencryption

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言