Android examples for java.security:AES
Decrypt Base64 encoded AES encrypted string
//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 { /**/* w ww . j ava2 s. c om*/ * Decrypt Base64 encoded encrypted string * * @param seed * @param encrypted * @return * @throws Exception */ public static String decrypt(String seed, String encrypted) throws Exception { byte[] rawKey = getRawKey(seed.getBytes()); byte[] enc = Base64.decode(encrypted.getBytes(), Base64.DEFAULT); byte[] result = decrypt(rawKey, enc); return new String(result); } private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; } 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; } }