简体   繁体   中英

URLClassLoader - Using an encrypted Jar

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?

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 ...
    . . .
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM