java.security.InvalidKeyException: Illegal key size exception during AES encryption

Reprinted from: http://www.cnblogs.com/milton/p/5058566.html


code

copy code
            // Set the encryption mode to CBC mode of AES 
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding" );
            SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
            IvParameterSpec iv = new IvParameterSpec(aesKey, 0, 16);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);

            // 加密
            byte[] encrypted = cipher.doFinal(unencrypted);
...
copy code

when executed to

cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);

, if the key is greater than 128, a java.security.InvalidKeyException: Illegal key size exception will be thrown. Because the key length is limited, the java runtime environment reads the limited policy file. The file is located in ${java_home }/jre/lib/security, this restriction is due to US controls on software export.

Solution: Download the JCE unrestricted permission policy file from the official website

JDK7 download address:  http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
JDK8 download address:  http://www.oracle.com/technetwork/java /javase/downloads/jce8-download-2133166.html is 
downloaded and decompressed, you can see local_policy.jar, US_export_policy.jar and readme.txt
If JRE is installed, put the two jar files in the %JRE_HOME%\lib\security directory
If the JDK is installed, the two jar files should also be placed in the %JDK_HOME%\jre\lib\security directory to overwrite the original files .



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325417459&siteId=291194637