加载加密JarFile通过URLCassloader [英] Loading Encrypted JarFile Via URLCassloader

查看:133
本文介绍了加载加密JarFile通过URLCassloader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在写一个系统来动态加载AES加密的jar文件。
我的代码:

  public static void main(String args [])throws Exception {

String jar =http://site.com/api/rsc/test.jar;
列表< URL> urls = new ArrayList< URL>();
urls.add(getURL(jar));
URL jarurl = urls.get(0);

ObjectInputStream ois = new ObjectInputStream((new URL(http://site.com/api/rsc/key_1.txt).openStream()));
Object o = ois.readObject();
DESKeySpec ks = new DESKeySpec((byte [])o);
SecretKeyFactory skf = SecretKeyFactory.getInstance(DES);
SecretKey key = skf.generateSecret(ks);

密码c = Cipher.getInstance(DES / CFB8 / NoPadding);
c.init(Cipher.DECRYPT_MODE,key,new IvParameterSpec((byte [])ois.readObject()));
CipherInputStream cis = new CipherInputStream((jarurl.openStream()),c);

JarInputStream jis = new JarInputStream(cis);
String main = jis.getManifest()。getMainAttributes()。getValue(Main-Class);
String classpaths [] = jis.getManifest()。getMainAttributes()。getValue(Class-Path)。split();

(String classpath:classpaths){
urls.add(getURL(classpath));
}

URLClassLoader loader = new URLClassLoader(urls.toArray(new URL [0]));
Class<?> cls = loader.loadClass(main);
Thread.currentThread()。setContextClassLoader(loader);
方法m = cls.getMethod(main,new Class [] {new String [0] .getClass()});
m.invoke(null,new Object [] {args});

}



一个简单的InputStream,我已经能够使用密码解密文件和读取内容。当我尝试运行一个简单的hello world应用程序,这是它抛出的错误:

 线程mainjava中的异常。 lang.ClassNotFoundException:java.net.URLClassLoader $ 1.run(URLClassLoader.java:202)上的helloworld.Main 
$ java.security.AccessController.doPrivileged(Native Method)上的

。 net.URLClassLoader.findClass(URLClassLoader.java:190)
在java.lang.ClassLoader.loadClass(ClassLoader.java:306)
在java.lang.ClassLoader.loadClass(ClassLoader.java:247)
在jarloader.JarLoader.main(JarLoader.java:63)

我错过了一些东西?谢谢你的时间。 =)

解决方案

您正在将加密的jar添加到URLClassLoader中。您希望URLClassLoader如何在加载jar时解密jar?



最好的方法是实现一个自定义的类加载器。扩展SecureClassLoader并实现相关方法。 ClassLoader javadoc中有一个基本示例。


I've been writing a little system to dynamically load AES encrypted jar files. My code:

public static void main(String args[]) throws Exception {

String jar = "http://site.com/api/rsc/test.jar";
List<URL> urls = new ArrayList<URL>();
urls.add(getURL(jar));
URL jarurl = urls.get(0);

ObjectInputStream ois = new ObjectInputStream((new URL("http://site.com/api/rsc/key_1.txt").openStream()));
Object o = ois.readObject();
DESKeySpec ks = new DESKeySpec((byte[])o);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey key = skf.generateSecret(ks);

Cipher c = Cipher.getInstance("DES/CFB8/NoPadding");
c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec((byte[]) ois.readObject()));
CipherInputStream cis = new CipherInputStream((jarurl.openStream()), c);

JarInputStream jis = new JarInputStream(cis);
String main = jis.getManifest().getMainAttributes().getValue("Main-Class");
String classpaths[] = jis.getManifest().getMainAttributes().getValue("Class-Path").split(" ");

for (String classpath: classpaths) {
    urls.add(getURL(classpath));
}

URLClassLoader loader = new URLClassLoader(urls.toArray(new URL[0]));
Class<?> cls = loader.loadClass(main);
Thread.currentThread().setContextClassLoader(loader);
Method m = cls.getMethod("main", new Class[]{new String[0].getClass()});
m.invoke(null, new Object[]{args});

}

This works fine with just a plain InputStream, and I have been able to decrypt files and read contents with the Cipher code before. WHen I try to run a simple hello world application, this is the error it throws:

Exception in thread "main" java.lang.ClassNotFoundException: helloworld.Main
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at jarloader.JarLoader.main(JarLoader.java:63)

Am I missing something? Thanks for your time. =)

解决方案

You are adding a url for your encrypted jar to the URLClassLoader. how do you expect the URLClassLoader to decrypt the jar when it loads it?

your best bet would be to implement a custom classloader. extend SecureClassLoader and implement the relevant methods. there's a basic example in the ClassLoader javadocs.

这篇关于加载加密JarFile通过URLCassloader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆