Http://www.2cto.com/kf/201312/264455.html
Causes
The company needs to encrypt a Web project after it is sold,
As we all know, class is good for anti-compilation,
So the class file needs to be encrypted first,
Then use your own classloader to decrypt and load.
Steps
It's about two steps:
1. Encrypt the class file
2. Write the ClassLoader to decrypt the class file and load it
3. Add this ClassLoader to Tomcat, which means that Tomcat can call into this ClassLoader
Encryption
1. Ideas
The byte stream reads the class file for a simple shift
2. Implement
Did a small program, implemented a folder under all the class file byte stream read, and 2-bit encryption method
3. Description
Swing is made using MyEclipse plug-ins, which can be a bit messy
4. Code & Downloads
The source code of the encryption program and the program are packaged into jar files uploaded here, double-click can be used.
"ClassLoader"
1. Code:
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677787980818283848586 |
package com.uikoo9.loader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.catalina.loader.WebappClassLoader;
/**
* 自定义的classloader
* 可以解密文件并加载
* @author uikoo9
*/
public class UClassLoader
extends WebappClassLoader{
/**
* 默认构造器
*/
public UClassLoader() {
super
();
}
/**
* 默认构造器
* @param parent
*/
public UClassLoader(ClassLoader parent) {
super
(parent);
}
/* (non-Javadoc)
* @see org.apache.catalina.loader.WebappClassLoader#findClass(java.lang.String)
*/
public Class<!--?--> findClass(String name)
throws ClassNotFoundException {
if
(name.contains(
"uikoo9"
)){
return findClassEncrypt(name);
}
else
{
return super
.findClass(name);
}
}
/**
* 查找class
* @param name
* @return
* @throws ClassNotFoundException
*/
private Class<!--?--> findClassEncrypt(String name)
throws ClassNotFoundException{
byte
[] classBytes =
null
;
try {
System.out.println(
"++++++" + name);
classBytes = loadClassBytesEncrypt(name);
}
catch (Exception e) {
e.printStackTrace();
}
Class<!--?--> cl = defineClass(name, classBytes,
0
, classBytes.length);
if (cl ==
null
)
throw new ClassNotFoundException(name);
return cl;
}
/**
* 加载加密后的class字节流
* @param name
* @return
* @throws IOException
*/
private byte
[] loadClassBytesEncrypt(String name)
throws IOException {
String basepath =
"Z:/program/workspaces/_work_03_bzb/WebRoot/WEB-INF/classes/"
;
// 项目物理地址
String cname = basepath + name.replace(
‘.‘
,
‘/‘
) +
".uikoo9"
;
System.out.println(cname);
FileInputStream in =
new FileInputStream(cname);
try {
ByteArrayOutputStream buffer =
new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -
1
) {
buffer.write((
byte
)(ch -
2
));
}
in.close();
return buffer.toByteArray();
}
finally {
in.close();
}
}
}
|
"Add to Tomcat"
1.context.xml
Locate the Tomcat under Contex.xml and add the following code between the context:
?
1 |
<loader delegate= "true" loaderclass= "com.uikoo9.loader.UClassLoader" ></loader> |
Where Loaderclass is to write loader,delegate= "true" means to decrypt only the non-system class and Jar
2. Add Loader
Put the loader class file you wrote in Tomcat\lib
Start
1. Use the encryption program to encrypt all the files under classes, after encryption all the class file suffix becomes uikoo9, you can modify the source code
2. Delete the original classes folder and copy the encrypted classes folder into
3. Modify Context.xml
Add Loader.class under 4.tomcat\lib
5. Start Tomcat
After the experiment is no problem, please leave a message if you have any questions.
Java EE encryption deployment, Tomcat uses its own ClassLoader decryption