AES 256加密问题

以下代码完美地实现了AES-128加密/解密。

public static void main(String[] args) throws Exception { String input = JOptionPane.showInputDialog(null, "Enter your String"); System.out.println("Plaintext: " + input + "\n"); // Generate a key KeyGenerator keygen = KeyGenerator.getInstance("AES"); keygen.init(128); byte[] key = keygen.generateKey().getEncoded(); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); // Generate IV randomly SecureRandom random = new SecureRandom(); byte[] iv = new byte[16]; random.nextBytes(iv); IvParameterSpec ivspec = new IvParameterSpec(iv); // Initialize Encryption Mode Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec); // Encrypt the message byte[] encryption = cipher.doFinal(input.getBytes()); System.out.println("Ciphertext: " + encryption + "\n"); // // Initialize the cipher for decryption cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec); // Decrypt the message byte[] decryption = cipher.doFinal(encryption); System.out.println("Plaintext: " + new String(decryption) + "\n"); } 

当我想使用AES-256时,我认为可以通过修改keygen.init(256);来完成keygen.init(256);byte[] iv = new byte[32]; ,但这会变成错误(线程中的exception“主”java.security.InvalidKeyException:非法密钥大小)! 有人可以解释为什么我做了这两个修改时会发生错误,我该怎么办。 感谢你们 :)

如果要使用AES 256加密,则必须安装无限强度管辖区域策略文件:

http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

这样可以实现更高的加密级别,如AES 256和RSA 2048。

将zip文件替换为\lib\security的当前文件。