Classloader使用及详解-java实现加密解密类加载器

自定义解密加密类加载器

接替上一篇文章的自定义类加载器,添加了解密加密的功能,实现解密加密的类加载器,这样以后源码可以通过加密后传输
加密解密

package com.ln.concurrent.classloader.chapter4;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.classloader.chapter4
 * @Name:SimpleEncrypt
 * @Author:linianest
 * @CreateTime:2021/1/6 14:39
 * @version:1.0
 * @Description TODO: 通过亦或的方式加密解密
 */
public class SimpleEncrypt {
    // 明文
    public static final String plain = "hello classLoader";
    // 秘钥
    public static final byte ENCRYPT_FACTOR = (byte) 0xff;

    //亦或的方式加密解密
    public static void main(String[] args) {
        // 加密
        final byte[] bytes = plain.getBytes();
        final byte[] encrypt = new byte[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            encrypt[i] = (byte) (bytes[i] ^ ENCRYPT_FACTOR);
        }
        System.out.println(new String(encrypt));
        // 解密
        final byte[] dencrypt = new byte[encrypt.length];
        for (int i = 0; i < encrypt.length; i++) {
            dencrypt[i] = (byte) (encrypt[i] ^ ENCRYPT_FACTOR);
        }
        System.out.println(new String(dencrypt));
    }

}

自定义解密加密累加载器

package com.ln.concurrent.classloader.chapter4;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.classloader.chapter4
 * @Name:EncryptUtils
 * @Author:linianest
 * @CreateTime:2021/1/6 14:47
 * @version:1.0
 * @Description TODO: 加密的工具类
 */
public abstract class EncryptUtils {
    // 秘钥
    private static final byte ENCRYPT_FACTOR = (byte) 0xff;

    private EncryptUtils() {
    }

    // 加密
    public static void doEncrypt(String source, String target) {
        try (final FileInputStream fileInputStream = new FileInputStream(source); final FileOutputStream fileOutputStream = new FileOutputStream(target)) {
            int data;
            while ((data = fileInputStream.read()) != -1) {
                fileOutputStream.write(data ^ ENCRYPT_FACTOR);
            }
            fileOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

解密

package com.ln.concurrent.classloader.chapter4;

import java.io.*;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.classloader.chapter4
 * @Name:DecryptClassLoader
 * @Author:linianest
 * @CreateTime:2021/1/6 15:06
 * @version:1.0
 * @Description TODO: 解密类加载器
 */
public class DecryptClassLoader extends ClassLoader {
    // 秘钥
    private static final byte ENCRYPT_FACTOR = (byte) 0xff;
    private static final String DEFULT_DIR = "E:\\";
    private String dir = DEFULT_DIR;

    public DecryptClassLoader() {
        super();
    }

    public DecryptClassLoader(ClassLoader parent) {
        super(parent);
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {

        final String classpath = name.replace(".", "/");
        final File classfile = new File(dir, classpath + ".class");
        if (!classfile.exists()) {
            throw new ClassNotFoundException("The class " + name + " not found under directory [" + dir + "]");
        }
        byte[] classBytes = loadClassBytes(classfile);
        if (null == classBytes || classBytes.length == 0) {
            throw new ClassNotFoundException("load the class " + name + " failed");
        }

        return this.defineClass(name, classBytes, 0, classBytes.length);
    }

    public void setDir(String dir) {
        this.dir = dir;
    }

    /**
     * 将文件读取到内存字节数组并加密后,输出流中
     * @param classfile
     * @return
     */
    private byte[] loadClassBytes(File classfile) {
        try(final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            final FileInputStream fileInputStream = new FileInputStream(classfile);){
            int data;
            while ((data = fileInputStream.read()) != -1) {
                byteArrayOutputStream.write(data ^ ENCRYPT_FACTOR);
            }
            byteArrayOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        }  catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值