Using encryption that would need Java Policy Files in openjre

[亡魂溺海] 提交于 2019-12-04 14:59:58

I found the following. It seems to solve all the policy problems i ever had.

  try {
    Field field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
    field.setAccessible(true);
    field.set(null, java.lang.Boolean.FALSE);
  } catch (Exception ex) {

  }

It's not nice but working!

The other answer is on the right lines but incomplete. After much screwing around, I hit on the following code which works for me. Don't worry about the legal aspects, Oracle actually has permission to get rid of this stupid export controls nonsense since 2011 but simply hasn't got around to updating the JVM yet! (check their bug tracker if you don't believe me).

Hope this helps. It works on Java 8. That part of the code is unlikely to change much, but obviously, we're fiddling with internal private code here so it could break at any time with any Java update. Caveat emptor.

Field gate = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
gate.setAccessible(true);
gate.setBoolean(null, false);
Field allPerm = Class.forName("javax.crypto.CryptoAllPermission").getDeclaredField("INSTANCE");
allPerm.setAccessible(true);
Object accessAllAreasCard = allPerm.get(null);
final Constructor<?> constructor = Class.forName("javax.crypto.CryptoPermissions").getDeclaredConstructor();
constructor.setAccessible(true);
Object coll = constructor.newInstance();
Method addPerm = Class.forName("javax.crypto.CryptoPermissions").getDeclaredMethod("add", java.security.Permission.class);
addPerm.setAccessible(true);
addPerm.invoke(coll, accessAllAreasCard);
Field defaultPolicy = Class.forName("javax.crypto.JceSecurity").getDeclaredField("defaultPolicy");
defaultPolicy.setAccessible(true);
defaultPolicy.set(null, coll);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!