DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Android Cloud Apps with Azure
  • How to Vaults and Wallets for Simple, Secure Connectivity
  • Performance Engineering Management: A Quick Guide
  • The Data Access Layer in Jmix: JPA on Steroids

Trending

  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Accelerating Debugging in Integration Testing: An Efficient Search-Based Workflow for Impact Localization
  1. DZone
  2. Data Engineering
  3. Data
  4. Secure Realm Encryption Key for Android Applications

Secure Realm Encryption Key for Android Applications

We take a look a the highly secure Realm database and it aids in encrypting the data from an Android application.

By 
julkar nain user avatar
julkar nain
·
Updated Apr. 08, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
10.0K Views

Join the DZone community and get the full member experience.

Join For Free

Nowadays, data security for Android applications is one of the main challenges for Android developers. A big part of data security involves the device's database. Android recommended an SQLite database but it will not provide any default encryption features for data security. Some database libraries provide default encryption features. Realm is one of those databases.

For encrypting a Realm database, you have to set an encryption key to the Realm configuration. This key will be used to encrypt/decrypt whole Realm databases. If anyone gets the encryption key, your data security will be vulnerable. So the encryption key should be secured first in order to secure the whole Realm database.

Realm encryption key can be secured in the following way:

  1. Use an Android provided key from the key store private certificate.

  2. Get a key from a remote source and secure it.

Method 1

Android OS provides a secure/private certificate through the Android keystore. It is an easy way to get a key from Android's private certificate.

public byte[] getSecureKey() {
        try {
            KeyStore.PrivateKeyEntry privateKeyEntry = getSecretKey();

            if (privateKeyEntry == null) {
                Log.d("key","key not found");

                return null;
            }
            Certificate cert = privateKeyEntry.getCertificate();

            if (cert == null) {
                Log.d("key","certificate not found");

                return null;
            }

            return cert.getEncoded();
        } catch (CertificateException e) {
            throw new RuntimeException(e);
        }
    }

Method 2

There's a problem with Method 1. If the keystore provided a private certificate that gets invalidated or corrupted for any reason (OS update or any bug), then all the data in the Realm DB will be corrupted. No data can be decrypted without a key. If data should not be lost on any condition, then Method 1 should not be used. In that case, Method 2 would be more appropriate. The following steps can be used in Method 2:

  • Generate a 64-byte random encryption key.

  • Encrypt the encryption key using the Android key store certificate and store it in the device's shared preferences.

  • Upload the encryption key to a remote source (it can be your own server or any cloud source).

    public byte[] getSecureRealmKey() {
        String key = getSharedPreference().getString(REALM_ENCRYPTION_KEY, null);

        if (TextUtils.isEmpty(key)) {
            return createRealmKey(context);
        }

        return encryptionProvider.decrypt(key);
    }

    private void setRealmKey(byte[] key) {
        SharedPreferences.Editor editor = getSharedPreference().edit();
        editor.putString(REALM_ENCRYPTION_KEY, encryptionProvider.encrypt(key));
        editor.apply();
    }

    private byte[] createRealmKey(Context context) {
        byte[] key = new byte[64];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(key);
        setRealmKey(key);

        return key;
    }

    private SharedPreferences getSharedPreference() {
        return context.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE);
    }


In case the Android keystore certificate gets invalidated, your data can be restored by using the remote key.
There is a third-party library that can help you to acheive this.

Android (robot) Data security application Database

Opinions expressed by DZone contributors are their own.

Related

  • Android Cloud Apps with Azure
  • How to Vaults and Wallets for Simple, Secure Connectivity
  • Performance Engineering Management: A Quick Guide
  • The Data Access Layer in Jmix: JPA on Steroids

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!