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)
        throws java.security.InvalidKeyException 

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)
            throws java.security.InvalidKeyException {
        try {//  w ww .j a v a 2s .  co 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(byte[] content, String key)
  2. decrypt(byte[] content, String password)
  3. decrypt(byte[] content, String password)
  4. decrypt(byte[] content, String password)
  5. decrypt(byte[] content, String password)
  6. decrypt(byte[] data, Key key)
  7. decrypt(byte[] data, Key key, String cipherAlgorithm)
  8. decrypt(byte[] data, byte[] key)
  9. decrypt(byte[] data, byte[] key, String cipherAlgorithm)