问题

网上的AES加解密工具类,基本没啥问题,但是一旦跑起来,极有可能出现问题。比如错误java.security.InvalidKeyException: Illegal key size
这种限制是因为美国对软件出口的控制。

解决方法

参考文章

https://blog.csdn.net/zlfprogram/article/details/83959920

方法一

下载 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files,覆盖上述目录下的对应jar文件(local_policy.jar, US_export_policy.jar)即可

下载地址:
JDK6 http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html
JDK7 http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
JDK8 http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

下载包的readme.txt 有安装说明,即替换
$
/jre/lib/security
$
/lib/security
目录下的 local_policy.jar 和 US_export_policy.jar 文件。

方法二

使用JDK 1.8.0_151 版本的会发现,在 $JAVA_HOME/jre/lib/security/ 目录下面多了一个 policy 文件夹,里面还有两个文件夹limited 和 unlimited;从Java 1.8.0_151开始,为JVM启用无限制强度管辖策略 有了一种新的更简单的方法。如果不启用此功能,则不能使用AES-256。
用文本编辑器打开java.security,并找到定义java安全性属性crypto.policy的行,它可以有两个值limited或unlimited,默认值是limited。
默认情况下,能找到一条注释掉的行:

#crypto.policy=unlimited

通过取消注释该行来启用无限制,删除# 然后重新启动Java应用程序即可。

AES加解密的工具类

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESUtil {

    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String KEY_ALGORITHM = "AES";

    private static final String KEY = "0b3e8fef7c79c7ed31f86d722aedc625";

    private static final String IV = "e5aff18fe6a2cf18";

    public static String encrypt(String plaintext) throws Exception {
        return encrypt(plaintext, KEY, IV);
    }

    public static String encrypt(String plaintext, String key, String iv) throws Exception {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String ciphertext) throws Exception {
        return decrypt(ciphertext, KEY, IV);
    }

    public static String decrypt(String ciphertext, String key, String iv) throws Exception {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
        return new String(decryptedBytes, "UTF-8");
    }

    public static void main(String[] args) throws Exception {
        String plaintext = "Hello, World!";
        String key = "0b3e8fef7c79c7ed31f86d722aedc625"; // 256-bit key (32 bytes)
        String iv = "e5aff18fe6a2cf18"; // 128-bit IV (16 bytes)
//
        String encryptedText = encrypt(plaintext, key, iv);
        System.out.println("Encrypted Text: " + encryptedText);

//        String decryptedText = decrypt(encryptedText, key, iv);

        System.out.println("key. length = " + key.length());
        System.out.println("iv. length = " + iv.length());

        String decryptedText = decrypt("qxgyARb6WlQw/L8rR/a1eA==", key, iv);
        System.out.println("Decrypted Text: " + decryptedText);
    }

}