AES de / encryption为256位密钥抛出“非法密钥大小”

时间:2016-09-11 18:08:17

标签: java encryption cryptography aes

我最近开始尝试AES en / decryption。 我的第一个实验来自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的操作的结果。

请注意,如果我用注释行交换键变量, (使用的示例中的原始128位密钥,)此代码工作正常。

有关为何抛出此异常以及如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:3)

您需要为JCE安装Unlimited Strength Jurisdiction Policy Files以使用256位密钥,请参阅:http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html