AES decrypt byte array and return byte array - Java Security

Java examples for Security:AES

Description

AES decrypt byte array and return byte array

Demo Code


//package com.java2s;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Main {


    /**//from  w w  w.j  a va 2  s.c o m
     * @param iv
     * @param key
     * @param text
     * @return
     */
    public static byte[] decrypt(byte[] iv, byte[] key, byte[] text) {

        try {

            AlgorithmParameterSpec algorithmParameterSpec = new IvParameterSpec(
                    iv);

            SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec,
                    algorithmParameterSpec);

            return cipher.doFinal(text);

        } catch (Exception exception) {

            return null;

        }

    }
}

Related Tutorials