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

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

Description

decrypt

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 {

    public static byte[] decrypt(byte[] content, String password) {
        try {// w  w w.j av a 2 s. c  o  m
            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 src)
  2. decrypt(String src)
  3. decrypt(byte[] content, String key)
  4. decrypt(byte[] content, String password)
  5. decrypt(byte[] content, String password)
  6. decrypt(byte[] content, String password)
  7. decrypt(byte[] content, String password)
  8. decrypt(byte[] data, Key key)
  9. decrypt(byte[] data, Key key, String cipherAlgorithm)