AES加密異常java.security.InvalidKeyException: Illegal key size 的解決方法


1. 項目使用AES加密,出現異常如下:
java.security.InvalidKeyException: Illegal key size

2. 為解決“AES的256位密鑰加解密報 java.security.InvalidKeyException: Illegal key size or default parameters 異常”問題:
需要使用oracle提供的無政策限制權限文件,在oracle官網上下載JDK對應版本的JCE文件,替換jre1.x\lib\security下面的local_policy.jar和
US_export_policy.jar兩個文件。

oracle無政策限制權限文件下載地址:鏈接:https://pan.baidu.com/s/1o2oY8NUZvEjmYkuT8iwu5w 密碼:y1ma。

注意:我的環境是JDK1.8,對用的策略文件也是1.8版本的。

文末附上我的加密算法:

package com.ehomepay.merchant.utils;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Objects;
import java.util.Optional;

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;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
* @date 2017/12/22
* comments AES加密工具類
*/
public class AESEncryptUtil {
/**
* 加密算法
*/
public static final String KEY_ALGORITHM = "AES";

public enum AES_KEY_SIZE {
/**
* 秘鑰長度為128
*/
KEY_LENGTH_128(128),
/**
* 密鑰長度為192
*/
KEY_LENGTH_192(192),
/**
* 秘鑰長度為256
*/
KEY_LENGTH_256(256);

AES_KEY_SIZE(final int size) {
this.keyLength = size;
}

private final int keyLength;

public int getKeyLength() {
return this.keyLength;
}
}

/**
* 加密算法,分組模式,填充模式
*/
public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";

public static byte[] generKey() {
return generKey(AES_KEY_SIZE.KEY_LENGTH_256);
}

public static byte[] generKey(final AES_KEY_SIZE keySize) {
try {
KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
AES_KEY_SIZE aesKeySize = Optional.ofNullable(keySize).orElse(AES_KEY_SIZE.KEY_LENGTH_128);
kgen.init(aesKeySize.getKeyLength());
SecretKey secretKey = kgen.generateKey();
return secretKey.getEncoded();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

public static String generKeyToBase64(final AES_KEY_SIZE keySize) {
return Base64.encodeBase64String(generKey(keySize));
}

public static String generKeyToBase64() {
return Base64.encodeBase64String(generKey());
}

protected static Key toKey(final byte[] key) {
return new SecretKeySpec(key, KEY_ALGORITHM);
}

protected static Key toKey(final String pwd) {
byte[] key = Base64.decodeBase64(pwd);
return toKey(key);
}


/**
* @param content 明文數據
* @param pwd Base64編碼的密鑰
* @return 加密數據
*/
public static byte[] encrypt(final byte[] content, final String pwd) {
return encryptService(content, pwd, Cipher.ENCRYPT_MODE);
}

/**
* @param content 明文數據
* @param pwd Base64編碼的密鑰
* @return 加密數據
*/
public static byte[] encrypt(final byte[] content, final byte[] pwd) {
return encryptService(content, pwd, Cipher.ENCRYPT_MODE);
}

/**
* @param content 密文數據
* @param pwd Base64編碼的密鑰
* @return 明文數據
*/
public static byte[] decrypt(final byte[] content, final String pwd) {
return encryptService(content, pwd, Cipher.DECRYPT_MODE);
}

/**
* @param content 密文數據
* @param pwd Base64編碼的密鑰
* @return 明文數據
*/
public static byte[] decrypt(final byte[] content, final byte[] pwd) {
return encryptService(content, pwd, Cipher.DECRYPT_MODE);
}

private static byte[] encryptService(final byte[] content, final byte[] pwd, int model) {
Objects.requireNonNull(content, "明文數據不能為空");
Objects.requireNonNull(pwd, "密鑰信息不能為空");
try {
initBC();
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(model, toKey(pwd));
return cipher.doFinal(content);
} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException
| IllegalBlockSizeException | BadPaddingException e) {
throw new RuntimeException(e);
}
}


private static byte[] encryptService(final byte[] content, final String pwd, int model) {
Objects.requireNonNull(content, "明文數據不能為空");
Objects.requireNonNull(pwd, "密鑰信息不能為空");
try {
initBC();
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(model, toKey(pwd));
return cipher.doFinal(content);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
| IllegalBlockSizeException | BadPaddingException | NoSuchProviderException e) {
throw new RuntimeException(e);
}
}

private static void initBC() {
Security.addProvider(new BouncyCastleProvider());
}


}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM