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

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

Description

decrypt

Parameter

Parameter Description
content a parameter
password a parameter

Declaration

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

Method Source Code

//package com.java2s;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

public class Main {
    /**//from w  w w . j  a  v a  2 s. c  om
     * 
     * @param content
     * @param password
     * @return
     */
    public static byte[] decrypt(byte[] content, String password) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(password.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

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