Reputation: 297
I have seen many Encrypted Class Loaders. Such as:
http://www.javaworld.com/javaworld/javaqa/2003-05/01-qa-0509-jcrypt.html?page=2
That one specifically is the one I am trying to adapt to my needs.
I have basically have an encrypted JAR that I have decrypted into a byte array ("byte[] decrypt;").
I now want to use that byte array to load the classes so I do not need to create a file on the hard drive containing the decrypted jar.
I am needing it to use URLClassLoader and NOT ClassLoader as I have another array ("URL[] urls") that the ClassLoader needs to take from. (Unless you can do this with a normal Class Loader?)
Any ideas?
Upvotes: 1
Views: 1981
Reputation: 30089
This seems pretty similar to this SO post:
Load a Byte Array into a Memory Class Loader
I think the only modification here is to take advantage of a parent classloader - so when you create an instance of your custom class loader, pass in a URLClassLoader to the constructor
public class MyClassLoader extends ClassLoader {
public MyClassLoader(URLClassLoader parent, byte[] decryptedBytes) {
super(parent);
this.decryptedBytes = decryptedBytes;
}
protected byte[] decryptedBytes;
public Class findClass(String name) {
byte[] b = loadClassData(name);
if (b != null) {
return defineClass(name, b, 0, b.length);
} else {
// delegate to parent URL classloader
getParent().findClass(name);
}
}
private byte[] loadClassData(String name) {
// load the class data from the connection
// use JarInputStream, find class, load bytes ...
. . .
}
}
Upvotes: 1