Java AES Descrypt AESDecrypt(byte[] encrypted, byte[] key, byte[] iv)

Here you can find the source of AESDecrypt(byte[] encrypted, byte[] key, byte[] iv)

Description

AES Decrypt

License

Apache License

Declaration

public static byte[] AESDecrypt(byte[] encrypted, byte[] key, byte[] iv) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w .j a  va2 s  .  co  m*/
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.security.InvalidKeyException;

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Main {

    public static byte[] AESDecrypt(byte[] encrypted, byte[] key, byte[] iv) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
            return cipher.doFinal(encrypted);
        } catch (InvalidKeyException e) {
            throw new IllegalArgumentException(
                    "if you see 'java.security.InvalidKeyException: Illegal key size', it cause by default JCE does not support 256-aes key for shipping reason.\n"
                            + "but you can download those two policy files"
                            + "      (US_export_policy.jar,local_policy.jar : Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files) "
                            + "and replace the same files in '$JDK_HOME/jre/lib/security',\n"
                            + "then it still work well. I would implement AES/CBC/NoPadding algorithm to make things better in futrue.\n"
                            + "download link:https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=jce_policy-6-oth-JPR@CDS-CDS_Developer",
                    e);
        } catch (Exception e) {
            throw new IllegalArgumentException("AESDecrypt failed.", e);
        }
    }
}

Related

  1. aesDecrypt(byte[] content, Key key)
  2. aesDecrypt(byte[] input, Key key)
  3. aesDecrypt(String encryptStr, String decryptKey)
  4. aesDecryptByBytes(byte[] encryptBytes, String decryptKey)