Android AES Decrypt decrypt(byte[] content, String key)

Here you can find the source of decrypt(byte[] content, String key)

Description

decrypt

Declaration

public static byte[] decrypt(byte[] content, String key) 

Method Source Code

//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    private static final String UTF_8 = "UTF-8";
    private static final byte[] defaultIV = { 127, 24, 123, 23, 93, 7, 15,
            0, 9, 4, 8, 15, 16, 23, 42, 1 };

    public static byte[] decrypt(byte[] content, String key) {
        try {/*from  ww  w . j av  a 2  s.  co m*/
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ivs = new IvParameterSpec(defaultIV);
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key), ivs);

            return cipher.doFinal(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static SecretKeySpec getSecretKey(String key) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("md5");
            SecretKeySpec keySpec = new SecretKeySpec(digest.digest(key
                    .getBytes(UTF_8)), "AES");
            return keySpec;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. decrypt(String seed, String encrypted)
  2. decrypt(String seed, String encrypted)
  3. decrypt(String seed, String encrypted)
  4. decrypt(String src)
  5. decrypt(String src)
  6. decrypt(byte[] content, String password)
  7. decrypt(byte[] content, String password)
  8. decrypt(byte[] content, String password)
  9. decrypt(byte[] content, String password)