zoukankan      html  css  js  c++  java
  • java自定义类加载器

    package lucene.baidu.tets;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class WriteByte {
    	/**
    	 * 转换四字节,此处主要加密使用可以自己使用加密算法,最后揭秘即可
    	 * @param iValue
    	 * @return
    	 */
    	public static byte[] Int2Bytes_LE(int iValue) {
    		byte[] rst = new byte[4];
    		// 先写int的最后一个字节
    		rst[0] = (byte) (iValue & 0xFF);
    		// int 倒数第二个字节
    		rst[1] = (byte) ((iValue & 0xFF00) >> 8);
    		// int 倒数第三个字节
    		rst[2] = (byte) ((iValue & 0xFF0000) >> 16);
    		// int 第一个字节
    		rst[3] = (byte) ((iValue & 0xFF000000) >> 24);
    		return rst;
    	}
    
    	public static void main(String[] args) throws IOException {
    
    		File file = new File("D:/资料/I盘/MyEclipse 10/Lucene_index/bin/lucene/baidu/tets/Application.class");//读class文件转换字节然后加密使用
    		FileChannel fout = new FileOutputStream("D:/资料/I盘/MyEclipse 10/Lucene_index/bin/lucene/baidu/tets/Application.class").getChannel();//写入文件
    		RandomAccessFile accessFile = new RandomAccessFile(file, "rw");
    		ByteBuffer bf = ByteBuffer.allocate((int) file.length() * 4);
    		bf.clear();
    		int read = -1;
    		while ((read = accessFile.read()) != -1) {
    			byte[] bytes = Int2Bytes_LE(read);
    			bf.put(bytes);
    		}
    		System.out.println(bf.limit());
    		bf.flip();
    		fout.write(bf);
    		fout.close();
    	}
    }

     application.java

    package lucene.baidu.tets;
    
    /**
     *    只返回一个句话 
     * @author wangnanhui
     *
     */
    public class Application {
        public static String getArr(){
            return "MyClassLoad hello world";
        }
        public static void main(String[] args) {
            System.out.println(Application.getArr());
        }
    }

    运行后得到加密后的文件,已经反编译不过来了

    解密代码

    package lucene.baidu.tets;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    class MyclassLoad extends ClassLoader {
    	/**
    	 * 解密方法
    	 * @param bytes
    	 * @return
    	 */
    	public int Bytes2Int_LE(byte[] bytes) {
    		if (bytes.length < 4)
    			return -1;
    		int iRst = (bytes[0] & 0xFF);
    		iRst |= (bytes[1] & 0xFF) << 8;
    		iRst |= (bytes[2] & 0xFF) << 16;
    		iRst |= (bytes[3] & 0xFF) << 24;
    
    		return iRst;
    	}
    
    	/**
    	 * 返回一个class 
    	 * @param filePath
    	 * @return
    	 */
    	public Class<?> getMyClass(String filePath) {
    		try {
    			File file = new File(filePath);
    			int length = (int) file.length();
    			FileChannel channel = new FileInputStream(file).getChannel();
    			ByteBuffer bytes = ByteBuffer.allocate(length);
    			ByteBuffer loader = ByteBuffer.allocate(length / 4);
    			byte[] read4 = new byte[4];
    			bytes.clear();
    			loader.clear();
    			channel.read(bytes);
    			bytes.flip();//开始读取 四个byte的读 , 然后解密到原来的byte
    			for (int i = 0; i < length; i += 4) {
    				bytes.get(read4);
    				loader.put((byte) Bytes2Int_LE(read4));
    			}
    		return super.defineClass(loader.array(), 0, length / 4);
    
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    	public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    		
    		MyclassLoad loader = new MyclassLoad();
    		Class<?> cls = loader.getMyClass("Application.class");
    		System.out.println(cls.getName());
    		Method method = cls.getMethod("getArr");
    		System.out.println(method.invoke(cls));
    		
    		
    		
    		
    	}
    }
    

      运行后

  • 相关阅读:
    Java ExecutorService四种线程池的例子与说明
    当代中国社会划分为十大阶层
    各大公司Java面试题超详细总结
    浅谈Java中的hashcode方法
    Java 9的JDK中值得期待的:不仅仅是模块化
    整理Excel数据,10秒搞定别人半天的工作量
    零基础搭建Hadoop大数据处理-环境
    Java性能优化权威指南第三章虚拟机概览第一部分
    15个顶级Java多线程面试题及答案
    最全面的Java多线程用法解析
  • 原文地址:https://www.cnblogs.com/wangnanhui/p/6828491.html
Copyright © 2011-2022 走看看