Encrypt plain string and encode to Base64 - Android java.security

Android examples for java.security:Password

Description

Encrypt plain string and encode to Base64

Demo Code


//package com.java2s;
import android.util.Base64;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    /**//from   w w w  . j  av a 2s.  c o m
     * Encrypt plain string and encode to Base64
     *
     * @param seed
     * @param plain
     * @return
     * @throws Exception
     */
    public static String encrypt(String seed, String plain)
            throws Exception {
        byte[] rawKey = getRawKey(seed.getBytes());
        byte[] encrypted = encrypt(rawKey, plain.getBytes());
        return Base64.encodeToString(encrypted, Base64.DEFAULT);
    }

    private static byte[] encrypt(byte[] raw, byte[] plain)
            throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] encrypted = cipher.doFinal(plain);
        return encrypted;
    }

    private static byte[] getRawKey(byte[] seed) throws Exception {
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(seed);
        keygen.init(128, random); // 192 and 256 bits may not be available
        SecretKey key = keygen.generateKey();
        byte[] raw = key.getEncoded();
        return raw;
    }
}

Related Tutorials