AES Encrypts plain text - Android java.security

Android examples for java.security:AES

Description

AES Encrypts plain text

Demo Code


//package com.java2s;
import android.util.Base64;
import java.security.SecureRandom;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    private static final int ITERATIONS = 1000;
    private static final int KEY_LENGTH = 256;
    private static final int SALT_LENGTH = 8;
    private static final String SECRET_KEY_ALGORITHM = "PBKDF2WithHmacSHA1";
    private static final String KEY_ALGORITHM = "AES";
    private static final String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
    private static final String TEXT_ENCODING = "UTF-8";
    private static final String SEPARATOR = ",";

    /**// w  ww.  j av  a  2  s. c o m
     * Encrypts plain text
     * @param password key password
     * @param text text to encrypt
     * @throws Exception
     */
    public static String encryptText(char[] password, String text)
            throws Exception {
        final SecureRandom secureRandom = new SecureRandom();
        byte[] salt = new byte[SALT_LENGTH];
        secureRandom.nextBytes(salt);

        final SecretKey secretKey = generateSecretKey(password, salt);
        final Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
        byte[] initVector = new byte[cipher.getBlockSize()];
        secureRandom.nextBytes(initVector);

        final IvParameterSpec ivParameterSpec = new IvParameterSpec(
                initVector);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
        byte[] data = cipher.doFinal(text.getBytes(TEXT_ENCODING));

        // Everything encrypted is also Base64-encoded, and concatenated by a separator (,) to ensure
        // that all of the information necessaryto decrypt resides in one single string.
        return Base64.encodeToString(data, Base64.NO_WRAP
                | Base64.NO_PADDING)
                + SEPARATOR
                + Base64.encodeToString(initVector, Base64.NO_WRAP
                        | Base64.NO_PADDING)
                + SEPARATOR
                + Base64.encodeToString(salt, Base64.NO_WRAP
                        | Base64.NO_PADDING);
    }

    /**
     * Generates a secret key
     * @param password passphrase for the key
     * @param salt a salt for the password
     */
    public static SecretKey generateSecretKey(char[] password, byte[] salt)
            throws Exception {
        final SecretKeyFactory secretKeyFactory = SecretKeyFactory
                .getInstance(SECRET_KEY_ALGORITHM);
        final KeySpec keySpec = new PBEKeySpec(password, salt, ITERATIONS,
                KEY_LENGTH);
        byte[] keyBytes = secretKeyFactory.generateSecret(keySpec)
                .getEncoded();
        return new SecretKeySpec(keyBytes, KEY_ALGORITHM);
    }
}

Related Tutorials