Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Data Encryption in Android Development

Introduction to Data Encryption

Data encryption is a method of converting data into a coded form to prevent unauthorized access. In Android development, encryption helps secure sensitive information such as user credentials, personal data, and application-specific information. This tutorial will guide you through the basics of data encryption in Android, including necessary concepts, libraries, and practical examples.

Understanding Encryption and Decryption

Encryption is the process of transforming readable data (plaintext) into an unreadable format (ciphertext) using an algorithm and a key. Decryption is the reverse process, where the ciphertext is converted back to plaintext using the same key.

Example:

If the plaintext is "Hello World" and the key is "key123", the encryption algorithm will transform it into a ciphertext like "U2FsdGVkX1+FzJpX5Yc=". The same key "key123" is used to decrypt this ciphertext back to "Hello World".

Types of Encryption

There are two main types of encryption used in Android development:

  • Symmetric Encryption: The same key is used for both encryption and decryption. Example algorithms include AES (Advanced Encryption Standard).
  • Asymmetric Encryption: Different keys are used for encryption and decryption. Example algorithms include RSA (Rivest-Shamir-Adleman).

Setting Up Encryption in Android

To use encryption in an Android application, you need to include the necessary libraries and set up the encryption logic. We will use the javax.crypto package for this purpose.

Example:

Add the following dependencies to your build.gradle file:

implementation 'org.bouncycastle:bcprov-jdk15on:1.68'

Symmetric Encryption Example with AES

Let's create a simple example to demonstrate symmetric encryption using the AES algorithm:

Example:

Here's a complete example in Java:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESEncryptionExample {

    public static void main(String[] args) throws Exception {
        String originalText = "Hello World";
        SecretKey secretKey = generateKey(128);
        
        String encryptedText = encrypt(originalText, secretKey);
        String decryptedText = decrypt(encryptedText, secretKey);
        
        System.out.println("Original Text: " + originalText);
        System.out.println("Encrypted Text: " + encryptedText);
        System.out.println("Decrypted Text: " + decryptedText);
    }

    private static SecretKey generateKey(int n) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(n);
        SecretKey key = keyGenerator.generateKey();
        return key;
    }

    private static String encrypt(String strToEncrypt, SecretKey secret) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
    }

    private static String decrypt(String strToDecrypt, SecretKey secret) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secret);
        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    }
}
                    

Asymmetric Encryption Example with RSA

Now, let's create a simple example to demonstrate asymmetric encryption using the RSA algorithm:

Example:

Here's a complete example in Java:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
import java.util.Base64;

public class RSAEncryptionExample {

    public static void main(String[] args) throws Exception {
        String originalText = "Hello World";
        
        KeyPair keyPair = generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        
        String encryptedText = encrypt(originalText, publicKey);
        String decryptedText = decrypt(encryptedText, privateKey);
        
        System.out.println("Original Text: " + originalText);
        System.out.println("Encrypted Text: " + encryptedText);
        System.out.println("Decrypted Text: " + decryptedText);
    }

    private static KeyPair generateKeyPair() throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);
        return keyGen.genKeyPair();
    }

    private static String encrypt(String strToEncrypt, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
    }

    private static String decrypt(String strToDecrypt, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    }
}
                    

Conclusion

Data encryption is a crucial aspect of Android development that ensures the security and privacy of user data. This tutorial covered the basics of encryption and decryption, types of encryption, and provided practical examples for both symmetric and asymmetric encryption methods. By incorporating encryption into your Android applications, you can protect sensitive information and build more secure apps.