This question already has answers here:
InvalidKeyException Illegal key size
                                
                                    (5个答案)
                                
                        
                                3年前关闭。
            
                    
我最近开始尝试AES加密/解密。
我的第一个实验是从here提取的,并做了一些修改。

然后,我尝试使用16位IV和256位密码(示例中使用的原始密码为128位,并且对我有用)生成加密的字符串,然后再次对其解密。
我还与this post进行了比较,但并没有真正的区别

如果我尝试运行下面提供的简单代码,则会不断收到此异常:

java.security.InvalidKeyException: Illegal key size


我的代码:

public static void main(String[] args) throws Exception {
        // byte [] key = "Bar12345Bar12345".getBytes("UTF-8"); // 128 bit key
        byte[] key = Hex.decode("8ec8f262e96e3d80ef52b530a5bc7b7baaf6e4357a363119b0a636b2034e298e");
        byte[] iv = Hex.decode("a5e8d2e9c1721ae0e84ad660c472c1f3");
        System.out.print(Arrays.toString(key));
        System.out.println(key.length);
        System.out.print(Arrays.toString(iv));
        System.out.println(iv.length);

        System.out.println(decrypt(key, iv, encrypt(key, iv, "Hello World")));

    }

    public static String encrypt(byte[] key, byte[] initVector, String value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector);
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");/// CBC/PKCS5PADDING
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] encrypted = cipher.doFinal(value.getBytes());
            System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted));

            return Base64.encodeBase64String(encrypted);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public static String decrypt(byte[] key, byte[] initVector, String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector);
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }


全输出:

[-114, -56, -14, 98, -23, 110, 61, -128, -17, 82, -75, 48, -91, -68, 123, 123, -86, -10, -28, 53, 122, 54, 49, 25, -80, -90, 54, -78, 3, 78, 41, -114]32
[-91, -24, -46, -23, -63, 114, 26, -32, -24, 74, -42, 96, -60, 114, -63, -13]16
java.security.InvalidKeyException: Illegal key size
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1034)
    at javax.crypto.Cipher.implInit(Cipher.java:800)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:859)
    at javax.crypto.Cipher.init(Cipher.java:1370)
    at javax.crypto.Cipher.init(Cipher.java:1301)
    at TestMain.encrypt(TestMain.java:76)
    at TestMain.main(TestMain.java:66)
java.security.InvalidKeyException: Illegal key size
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1034)
    at javax.crypto.Cipher.implInit(Cipher.java:800)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:859)
    at javax.crypto.Cipher.init(Cipher.java:1370)
    at javax.crypto.Cipher.init(Cipher.java:1301)
    at TestMain.decrypt(TestMain.java:95)
    at TestMain.main(TestMain.java:66)
null


请注意,输出中的最后一个'null'是已在main的最后一行中打印到System.out的操作的结果。

另请注意,如果我将key变量替换为带注释的行,
(使用示例中的原始128位密钥),此代码可以正常工作。

关于为何引发此异常以及如何解决此异常的任何想法?

最佳答案

您需要安装JCE的“无限强度权限策略文件”才能使用256位密钥,请参阅:http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

07-27 20:28