简体   繁体   English

URLClassLoader-使用加密的Jar

[英]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 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;"). 我基本上有一个加密的JAR,已将其解密为字节数组(“ byte []解密;”)。

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. 现在,我想使用该字节数组加载类,因此无需在包含已解密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. 我需要它使用URLClassLoader而不是ClassLoader,因为我有另一个需要从ClassLoader中获取的数组(“ URL [] urls”)。 (Unless you can do this with a normal Class Loader?) (除非您可以使用普通的类加载器执行此操作?)

Any ideas? 有任何想法吗?

This seems pretty similar to this SO post: 这似乎非常类似于此SO帖子:

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 我认为这里唯一的修改是利用父类加载器-因此,当您创建自定义类加载器的实例时,请将URLClassLoader传递给构造函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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