使用Bouncy Castle的AES 256bit加密:仍然需要无限强度政策?

时间:2012-10-15 11:49:12

标签: java encryption aes bouncycastle

我想使用带有Bouncy Castle的AES 256位加密,我想知道尽管BC仍然需要“Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files”,因为我收到以下java.security.InvalidKeyException: Illegal key size例外情况代码:

public class AES256 {
    public static void main(String[] args) throws Exception {
        Security.addProvider(new BouncyCastleProvider());

        final KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(256); // doesn't work for 192, too

        final byte[] encoded = keyGen.generateKey().getEncoded();

        final SecretKeySpec keySpec = new SecretKeySpec(encoded, "AES");
        final Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
        // Please ignore static IV for this example
        final IvParameterSpec iv = new IvParameterSpec(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});

        c.init(Cipher.ENCRYPT_MODE, keySpec, iv); // throws java.security.InvalidKeyException: Illegal key size
    }
}

我错过了什么?有没有办法在没有无限强度政策文件的情况下使用256位密钥?

1 个答案:

答案 0 :(得分:5)

bouncycastle FAQ中的第一个问题。