字节码加密(三)jar包解密后删除

在字节码加密(一)jar包加密https://www.cnblogs.com/silyvin/articles/11767818.html中

删除jar包是用delOnExit操作,但实际上是任务型的项目,跑完加密jar包的任务时,我们的jvm不会关闭,那解密的jar包就泄露了

因此我们要把jar包删除的操作放在类加载器finalize时

 

        Thread.sleep(5000);

myUrlClassLoader = null;
System.gc();

Thread.sleep(5000);
}

private static class MyUrlClassLoader {

URL [] murls;

private URLClassLoader urlClassLoader;

private static Coder coder = new Use3DES();

public MyUrlClassLoader(URL[] urls) {
URL [] urls1 = decode(urls);
urlClassLoader = new URLClassLoader(urls1);
// delUrls(urls1);
murls = urls1;
}

@Override
protected void finalize() throws Throwable {
System.out.println("回收decJar类加载器");
for(URL url : murls) {
File file = new File(url.getFile());
file.delete();
}
super.finalize();
}

@Deprecated
private static void delUrls(URL[] urls) {
for(URL url : urls) {
File file = new File(url.getFile());
file.deleteOnExit();
}
}

private static URL[] decode(URL [] urls) {
URL[] outUrls = new URL[urls.length];
for(int i=0; i<outUrls.length; ++i) {
URL url = urls[i];

String sin = url.getFile();
String sout = sin + ".dec.jar";
coder.decode(sin, sout);

try {
outUrls[i] = new URL("file:" + sout);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return outUrls;
}

public Class<?> loadClass(String name) throws ClassNotFoundException {
return urlClassLoader.loadClass(name);
}

public ClassLoader getParent() {
return urlClassLoader.getParent();
}
}
}

 

parent:--sun.misc.Launcher$AppClassLoader@119d7047
C:--java.net.URLClassLoader@71be98f5
子 C 加载
父 D 加载
子 E 加载
回收decJar类加载器。 5秒后打印

posted on 2019-11-29 14:41  silyvin  阅读(281)  评论(0编辑  收藏  举报