Memorydoc
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
    • HTML
    • CSS
    • 前端拓展
  • 编程之道

    • 并发编程
    • 设计模式
    • 数据结构算法
    • 技术拓展
    • 技术陷阱
    • 面试宝典
  • 分布式

    • 微服务
    • 数据库
  • 项目优化实战

    • JVM 优化
    • 线程池优化
    • 模板引擎优化
    • 任务调度优化
    • 内存优化
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Memorydoc

术尚可求
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
    • HTML
    • CSS
    • 前端拓展
  • 编程之道

    • 并发编程
    • 设计模式
    • 数据结构算法
    • 技术拓展
    • 技术陷阱
    • 面试宝典
  • 分布式

    • 微服务
    • 数据库
  • 项目优化实战

    • JVM 优化
    • 线程池优化
    • 模板引擎优化
    • 任务调度优化
    • 内存优化
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 并发编程

  • 设计模式

  • 数据结构算法

  • 技术拓展

    • 服务器搭建https
    • Maven 知识拓展
    • 项目集成Mybatis
    • 添加多个Ssh Key到一台电脑
    • BIO、NIO、AIO、Netty
    • 类加解密 & 自定义类加载器
      • 类加解密工具
      • 自定义类加载器
      • 测试类
        • 测试结果
    • 优化web 项目性能
    • 提升万倍系统吞吐量
    • 优雅写代码一
    • 优雅写代码二
    • 优雅写代码三
  • 技术陷阱

  • 面试宝典

  • 微服务

  • 数据库

  • 项目优化背景

  • JVM优化

  • 技术架构
  • 技术拓展
Memorydoc
2022-03-07

类加解密 & 自定义类加载器

# 类加解密工具

package com.xiangxue.classloader;

import java.io.*;

/**
 * @version 1.0
 * @program: springTest
 * @description: 类的加解密工具
 * @author: Kevin
 * @create: 2019-09-10 14:00
 **/
public class EDClassUtils {

    ///异或操作, 可以进行加密、解密
    public void xor(InputStream inputStream, OutputStream outputStream) throws IOException {
        int ch;
        while (-1 != (ch = inputStream.read())){
            ch = ch^ 0xff;
            outputStream.write(ch);
        }
    }

    public void encrypt(File src, File dest) throws IOException {
        InputStream inputStream = new FileInputStream(src);
        OutputStream outputStream = new FileOutputStream(dest);
        try {
            xor(inputStream, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            inputStream.close();
            outputStream.close();
        }
    }

    //解密方法
    public byte[] decode(File src) throws IOException {
        InputStream inputStream = new FileInputStream(src);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xor(inputStream, byteArrayOutputStream);
        byte[] data = byteArrayOutputStream.toByteArray();
        return data;
    }


    public static void main(String[] args) {
        EDClassUtils edClassUtils = new EDClassUtils();
        File src = new File("D:\\gitcloneTest\\springTest\\target\\classes\\com\\xiangxue\\io\\aio\\ServerEx.class");
        File dest = new File("D:\\gitcloneTest\\springTest\\target\\classes\\com\\xiangxue\\io\\aio\\Server.class");
        System.out.println("加密完成");
        try {
            edClassUtils.encrypt(src, dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            edClassUtils.decode(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

# 自定义类加载器

package com.xiangxue.classloader;

import java.io.File;
import java.io.IOException;

/**
 * @version 1.0
 * @program: springTest
 * @description: 自定义的类加载器
 * @author: Kevin
 * @create: 2019-09-10 14:02
 **/
public class CustomClassLoader extends ClassLoader {

    private String bashPath;
    private static final String FILE_EXT = ".class";

    public CustomClassLoader(String bashPath) {
        this.bashPath = bashPath;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] data = this.loadClassData(name);
        return this.defineClass(name, data, 0, data.length);
    }

    private byte[] loadClassData(String name) {
        byte[] data = null;
        EDClassUtils edClassUtils = new EDClassUtils();
        String tempName = name.replaceAll("\\.", "\\\\");
        try {
            data = edClassUtils.decode(new File(bashPath + tempName + FILE_EXT));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# 测试类

package com.xiangxue.classloader;

/**
 * @version 1.0
 * @program: springTest
 * @description:
 * @author: Kevin
 * @create: 2019-09-10 14:20
 **/
public class Test {
    public static void main(String[] args) {
        CustomClassLoader customClassLoader =new CustomClassLoader("D:\\gitcloneTest\\springTest\\target\\classes\\");
        try {
            Class<?> serverEx = customClassLoader.findClass("com.xiangxue.io.aio.Server");
            System.out.println(serverEx.getClassLoader());
            Object o = serverEx.newInstance();
            System.out.println(o);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 测试结果

com.xiangxue.classloader.CustomClassLoader@63947c6b
com.xiangxue.io.aio.Server@355da254
1
2
编辑 (opens new window)
上次更新: 2022/03/08, 00:59:33
BIO、NIO、AIO、Netty
优化web 项目性能

← BIO、NIO、AIO、Netty 优化web 项目性能→

最近更新
01
命令模式 原创
05-03
02
桥接模式 原创
05-02
03
优雅写代码三 原创
04-29
更多文章>
Theme by Memorydoc | Copyright © 2021-2022 Memorydoc | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式