java error: Illegal key size or default parameters

Recently, AES is used for encryption. There was no problem after the test, but after updating the key, the following error occurred.

Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
	at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1026)
	at javax.crypto.Cipher.implInit(Cipher.java:801)
	at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
	at javax.crypto.Cipher.init(Cipher.java:1249)
	at javax.crypto.Cipher.init(Cipher.java:1186)
	at com.cupdata.oam.cardapply.kit.AesKit.decrypt(AesKit.java:94)
	... 3 more

The reason why this kind of problem occurs is that we need to understand a new thing-JCE. There is a JCE (Java Cryptography Extension) in Java's core class library. JCE is a set of packages that provide the framework and implementation for encryption, key generation and negotiation, and Message Authentication Code (MAC) algorithm, so this is the implementation An important class library for encryption and decryption.

The reason for the error Illegal key size or default parameters on the Liunx server is that Java can find corresponding implementations for almost all commonly used encryption algorithms. Because of the export restrictions of the United States, Sun made corresponding restrictions through the permission files (local_policy.jar, US_export_policy.jar). Therefore, there are some problems: The key length cannot meet the requirements (such as: java.security.InvalidKeyException: Illegal key size or default parameters)

We just need to download these two files from the Internet and replace them.

Problem Solving
Through the above description, we know where the problem lies, and start to solve it below. What we have to do is to replace the JCE library. The file location is under your JDK/jre/lib/security directory. In this directory, we can see two jar packages: local_policy.jar, US_export_policy.jar, these two jars The package comes with jdk. We need to download the jar package that supports 256-bit key encryption.

JDK8:

The corresponding JCE download address is: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

After downloading, unzip and replace the two files "local_policy.jar" and "US_export_policy.jar" with your own

Extension:
The real project development must be in Linux. If there is, just replace it directly. If there are no these two JAR packages in the directory above JDK1.8. Our solution is: open java.securitythe file with the vim command, open the commented out  #crypto.policy=unlimited , and then save it

Guess you like

Origin blog.csdn.net/zlfjavahome/article/details/130614942