可以从内存中 class 的反射创建对象吗?

Possible to create object from reflection of a class in memory?

我有一个正在编写的程序,我试图在其中获取加密的 class 文件,在加载到内存中时对其进行解密(而不是在驱动器上对其进行解密),然后执行部分class。我可以用静态方法做到这一点,但是,我想知道是否可以做到这一点

我也想说明一下,我没有写任何恶意的东西。实际上,我这样做是为了尝试理解反射,并可能将其用作一种源代码保护形式。有效载荷对于我的概念证明来说似乎是一个令人满意的名字。

@SuppressWarnings("resource")
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter password:");
    String pass = scan.nextLine();

    MessageDigest md5 = MessageDigest.getInstance("md5");
    byte[] keyBytes = md5.digest(pass.getBytes("UTF-8"));
    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(iv);


    InputStream in = Master.class.getClassLoader().getResourceAsStream("Payload\ENC\Payload.class");

    int read = 0;
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    while ((read = in.read(buffer)) != -1)
    {
        boas.write(buffer, 0, read);
    }
    byte[] encrypted = boas.toByteArray();


    try
    {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5padding");
        cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
        byte[] decrypted = cipher.doFinal(encrypted);

        PayloadLoader loader = new PayloadLoader(Thread.currentThread().getContextClassLoader(), decrypted);
        Class<?> c = loader.loadClass("Payload.Payload");

        Method main = c.getMethod("main", String[].class);
        main.invoke(null, (Object) args);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.out.println("Invalid Password");
    }

上面是加载到内存并执行的地方。但是,我不想调用 Main 方法,而是想创建该线程的一个实例并启动它。下面是基本线程本身

private Thread t;
private String threadName;

Payload(String name){
    threadName = name;
    if(config.DEBUG)
        System.out.println("Creating Thread - " + threadName);
}

public void setName(String name){
    threadName = name;
}

public void run()
{
    if(config.DEBUG)
        System.out.println("Running Thread - " + threadName);


}

public void start()
{
    if(config.DEBUG)
        System.out.println("Starting Thread - " + threadName);

    if(t == null)
    {
        t = new Thread (this, threadName);
        t.start();
    }
}

我的问题是,是否可以创建这个对象并启动线程本身?这是我要完成的主要任务,因为我希望它是多线程的。

Class class 有 getConstructors 方法可以使用:

Constructor<?> constructor = c.getConstructor(String.class);
Object payload = constructor.newInstance("ThreadName");
((PayloadStarter) payload).start();

Payload class 还需要实现代码 class 路径中的共享接口,以便转换和调用 start 方法.我在上面的例子中使用了虚构的 PayloadStarter