| package cc.mrbird.febs.pay.util; | 
|   | 
| import javax.crypto.Cipher; | 
| import java.lang.reflect.Field; | 
| import java.lang.reflect.Modifier; | 
| import java.security.NoSuchAlgorithmException; | 
| import java.security.Permission; | 
| import java.security.PermissionCollection; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * 解决因jdk版本问题不支持aes256加密问题(Illegal key size or default parameters) | 
|  */ | 
| public class JCEUtil { | 
|     public static void removeCryptographyRestrictions() { | 
|         if (!isRestrictedCryptography()) { | 
|             System.out.println("Cryptography restrictions removal not needed"); | 
|             return; | 
|         } | 
|         try { | 
|             /* | 
|              * Do the following, but with reflection to bypass access checks: | 
|              * | 
|              * JceSecurity.isRestricted = false; | 
|              * JceSecurity.defaultPolicy.perms.clear(); | 
|              * JceSecurity.defaultPolicy.add(CryptoAllPermission.INSTANCE); | 
|              */ | 
|             final Class<?> jceSecurity = Class.forName("javax.crypto.JceSecurity"); | 
|             final Class<?> cryptoPermissions = Class.forName("javax.crypto.CryptoPermissions"); | 
|             final Class<?> cryptoAllPermission = Class.forName("javax.crypto.CryptoAllPermission"); | 
|   | 
|             final Field isRestrictedField = jceSecurity.getDeclaredField("isRestricted"); | 
|             isRestrictedField.setAccessible(true); | 
|             //去除isRestricted的final限制 | 
|             final Field modifiersField = Field.class.getDeclaredField("modifiers"); | 
|             modifiersField.setAccessible(true); | 
|             modifiersField.setInt(isRestrictedField, isRestrictedField.getModifiers() & ~Modifier.FINAL); | 
|             isRestrictedField.set(null, false); | 
|   | 
|             final Field defaultPolicyField = jceSecurity.getDeclaredField("defaultPolicy"); | 
|             defaultPolicyField.setAccessible(true); | 
|             final PermissionCollection defaultPolicy = (PermissionCollection) defaultPolicyField.get(null); | 
|   | 
|             final Field perms = cryptoPermissions.getDeclaredField("perms"); | 
|             perms.setAccessible(true); | 
|             ((Map<?, ?>) perms.get(defaultPolicy)).clear(); | 
|   | 
|             final Field instance = cryptoAllPermission.getDeclaredField("INSTANCE"); | 
|             instance.setAccessible(true); | 
|             defaultPolicy.add((Permission) instance.get(null)); | 
|   | 
|         } catch (final Exception e) { | 
|             e.printStackTrace(); | 
|         } | 
|     } | 
|   | 
|     public static boolean isRestrictedCryptography() { | 
|         // This matches Oracle Java 7 and 8, but not Java 9 or OpenJDK. | 
|         final String name = System.getProperty("java.runtime.name"); | 
|         final String ver = System.getProperty("java.version"); | 
|         return name != null && name.equals("Java(TM) SE Runtime Environment") | 
|                 && ver != null && (ver.startsWith("1.7") || ver.startsWith("1.8")); | 
|     } | 
|   | 
|     public static void main(String[] args) throws NoSuchAlgorithmException { | 
| //        removeCryptographyRestrictions(); | 
|         long length = Cipher.getMaxAllowedKeyLength("AES/CBC/PKCS5Padding"); | 
|         System.out.println(length); | 
|     } | 
| } |