我想对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 位密钥?