How to Encrypt Data in Android: A Beginner’s Guide

Mouad Oumous
Javarevisited
Published in
4 min read1 day ago

Encryption sounds like one of those scary tech terms, right? But if you’re building an Android app, especially one dealing with sensitive data, encryption is a must. It’s basically the process of transforming your data into gibberish that only authorized users can decode. In this article, I’ll walk you through the basics of encryption on Android and how you can use the Android Keystore to keep your data safe.

Let’s get started!

Why Encryption Matters in Android

First, why do we care? Apps are increasingly dealing with sensitive information: passwords, personal info, banking details, etc. You don’t want anyone with access to a phone (or worse, a hacker) to be able to peek into this data. That’s where encryption steps in, turning your data into unreadable chunks unless you have the right key to decrypt it.

In short: encryption ensures that your data is safe, even if someone gains unauthorized access.

Android Keystore: Your Secure Key Vault

Android offers a secure place to store cryptographic keys — the Android Keystore. This is a big deal because, with the Keystore, you can store encryption keys in a way that prevents them from being extracted. Even if your app gets hacked, the keys are safe.

Step-by-Step Encryption

Now let’s walk through how to encrypt and decrypt data in Android using AES (Advanced Encryption Standard), one of the most widely used encryption algorithms. We’ll store the encryption key in the Android Keystore.

Step 1: Generate and Store the Key

The first thing we need is an encryption key. This key is what will allow us to encrypt and decrypt data. Here’s how you can generate a key and store it securely in the Keystore:

import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class EncryptionHelper {
private static final String KEY_ALIAS = "MyEncryptionKey"; // Unique alias for your key
public static void generateKey() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
KEY_ALIAS,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT
).setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.build();
keyGenerator.init(keyGenParameterSpec);
keyGenerator.generateKey();
} catch (Exception e) {
e.printStackTrace();
}
}
}

This code generates an AES key and stores it securely in the Android Keystore under the alias `MyEncryptionKey`.

Step 2: Encrypt Your Data

Now that we have a key, we can encrypt our data. Let’s say you have a sensitive string, like a password, that you want to encrypt.

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import java.security.KeyStore;
import java.util.Base64;

public class EncryptionHelper {
private static final String KEY_ALIAS = "MyEncryptionKey";
private static final String TRANSFORMATION = "AES/GCM/NoPadding";
public static String encrypt(String plainText) {
try {
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
SecretKey secretKey = (SecretKey) keyStore.getKey(KEY_ALIAS, null);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] iv = cipher.getIV(); // Initialization Vector
byte[] encryption = cipher.doFinal(plainText.getBytes("UTF-8"));
// Combine IV and the encrypted data
byte[] combined = new byte[iv.length + encryption.length];
System.arraycopy(iv, 0, combined, 0, iv.length);
System.arraycopy(encryption, 0, combined, iv.length, encryption.length);
return Base64.getEncoder().encodeToString(combined);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

- Initialization Vector (IV): This is a random piece of data added to the encryption process to make sure the same plain text doesn’t result in the same encrypted output each time.
- The result is then Base64 encoded so we can store or send it easily.

Step 3: Decrypt the Data

Decrypting the data is just the reverse of encryption. The same key that encrypted the data will decrypt it. Here’s how you can do it:

public static String decrypt(String encryptedText) {
try {
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);

SecretKey secretKey = (SecretKey) keyStore.getKey(KEY_ALIAS, null);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
// Extract IV from the encrypted text
byte[] decoded = Base64.getDecoder().decode(encryptedText);
byte[] iv = new byte[12]; // GCM IV size is 12 bytes
System.arraycopy(decoded, 0, iv, 0, iv.length);
GCMParameterSpec spec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);
// Extract the encrypted data
byte[] encryptedData = new byte[decoded.length - iv.length];
System.arraycopy(decoded, iv.length, encryptedData, 0, encryptedData.length);
byte[] decryptedBytes = cipher.doFinal(encryptedData);
return new String(decryptedBytes, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

In decryption, you first extract the IV from the encrypted text, then use it to initialize the cipher. Finally, the `cipher.doFinal` method gives you back the original plain text.

And That’s It!

You’ve just encrypted and decrypted data on Android using the Keystore. This method helps you securely handle sensitive information like passwords, API keys, or any private user data that should not be exposed.

Why Use the Android Keystore?

- Security: Keys stored in the Keystore cannot be extracted, making it much harder for attackers to steal them.
- Convenience: You don’t have to worry about storing or managing the encryption keys yourself.

Key Takeaways:

- Always use encryption if your app handles sensitive data.
- The Android Keystore is your best friend for securely managing encryption keys.
- With encryption, even if your data is accessed by unauthorized users, they won’t be able to read it without the right key.

Happy coding! Stay secure!

--

--

Mouad Oumous
Javarevisited

I'm a blogger on medium, l'm a java and kotlin developer, also an Android Developer I mastered various topics such as Networking, UI , UX, Animation ...