JAva暗号化class

2360 ワード

package csdn.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class MyClassLoader extends ClassLoader {

	public static final int CLASS_LOADER_CHAR = 0X12FFAADE ; 
	private String fname ;

	public MyClassLoader() {

	}

	public MyClassLoader(String fname) {
		this.fname = fname;
	}

	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		String clazzName = name.indexOf(".") != -1 ? name.substring(name
				.lastIndexOf(".") + 1) : name;
		byte[] bs = decrypt(new File(fname + "\\" + clazzName + ".class"));
		return super.defineClass(name, bs, 0, bs.length);
	}

	public static void main(String[] args) throws Exception {
		File readFile = new File(
				"WebRoot\\WEB-INF\\classes\\com\\entity\\Person.class");
		File writeFile = new File("booklib");
		encrypt(readFile, writeFile);
		Class<?> clazz = new MyClassLoader("booklib") 
				.findClass("com.entity.Person") ;  
		System.out.println( clazz.newInstance() ) ; 
	}

	static byte[] decrypt(File writeFile) {
		try {
			if (!writeFile.exists()) {
				return null;
			}
			InputStream is = new FileInputStream(writeFile);
			byte[] b = new byte[1];
			ByteArrayOutputStream bos = new ByteArrayOutputStream();

			int len = b.length;
			while (is.read(b) != -1) {
				for (int x = 0; x < len; x++) {
					b[x] = (byte) (b[x] ^ CLASS_LOADER_CHAR);
				}
				bos.write(b);
			}
			is.close();
			byte[] bytes = bos.toByteArray();
			bos.close();
			return bytes;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	static boolean encrypt(File readFile, File writeFile) {
		if (!readFile.exists()) {
			return false;
		}
		try {
			InputStream is = new FileInputStream(readFile);
			OutputStream os = new FileOutputStream(writeFile + "\\"
					+ readFile.getName());
			byte[] b = new byte[1];
			int len = b.length;
			while (is.read(b) != -1) {
				for (int x = 0; x < len; x++) {
					b[x] = (byte) (b[x] ^ CLASS_LOADER_CHAR);
				}
				os.write(b);
			}
			is.close();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}