Android examples for java.security:AES
AES Decrypt CBC
import java.io.UnsupportedEncodingException; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import android.util.Base64; public class Main { public static String DecryptCBC(String stringToEncode, String Key, String IV) throws Exception { try {/* w ww.j av a 2 s . com*/ if (Key == null) { return null; } byte[] decode = Base64.decode(stringToEncode, 0); IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes()); SecretKeySpec skeySpec = getKey(Key); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec); byte[] ret = cipher.doFinal(decode); return new String(ret, "utf-8"); } catch (Exception ex) { System.out.println(ex.toString()); return null; } } private static SecretKeySpec getKey(String Key) throws UnsupportedEncodingException { int keyLength = 256; byte[] keyBytes = new byte[keyLength / 8]; Arrays.fill(keyBytes, (byte) 0x0); byte[] passwordBytes = Key.getBytes("UTF-8"); int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length; System.arraycopy(passwordBytes, 0, keyBytes, 0, length); SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); return key; } }