对jar或者war进行加密解密
whycxzp
2021-12-22 f6b935781bcb43faea7aa894ce3a55873769efb3
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.whyc;
 
 
import com.whyc.util.*;
 
import java.io.Console;
import java.io.File;
import java.lang.instrument.Instrumentation;
import java.nio.charset.Charset;
 
 
/**
 * 监听类加载
 *
 * @author perry
 */
public class CoreAgent {
    /**
     * man方法执行前调用
     *
     * @param args 参数
     * @param inst inst
     */
    public static void premain(String args, Instrumentation inst) {
        //Const.pringInfo();
        CmdLineOption options = new CmdLineOption();
        options.addOption("pwd", true, "密码");
        options.addOption("pwdname", true, "环境变量密码参数名");
        options.addOption("nopwd", false, "无密码启动");
        options.addOption("debug", false, "调试模式");
        options.addOption("del", true, "读取密码后删除密码");
 
        char[] pwd;
 
        //读取jar隐藏的密码,无密码启动模式(jar)
        //pwd = JarDecryptor.readPassFromJar(new File(JarUtils.getRootPath(null)));
        pwd = JarDecryptor.readPassFromJar2(new File(JarUtils.getRootPath(null)));
 
        if (args != null) {
            options.parse(args.split(" "));
            Const.DEBUG = options.hasOption("debug");
        }
 
        //参数标识 无密码启动
        if (options.hasOption("nopwd")) {
            pwd = new char[1];
            pwd[0] = '#';
        }
 
        //参数获取密码
        if (StrUtils.isEmpty(pwd)) {
            pwd = options.getOptionValue("pwd", "").toCharArray();
        }
 
        //参数没密码,读取环境变量中的密码
        if (StrUtils.isEmpty(pwd)) {
            String pwdname = options.getOptionValue("pwdname");
            if (StrUtils.isNotEmpty(pwdname)) {
                String p = System.getenv(pwdname);
                pwd = p == null ? null : p.toCharArray();
            }
        }
 
        //参数、环境变量都没密码,读取密码配置文件
        if (StrUtils.isEmpty(pwd)) {
            Log.debug("无法从GUI中获取密码,读取密码文件");
            pwd = readPasswordFromFile(options);
        }
 
        // 配置文件没密码,从控制台获取输入
        if (StrUtils.isEmpty(pwd)) {
            Log.debug("无法在参数中获取密码,从控制台获取");
            Console console = System.console();
            if (console != null) {
                Log.debug("控制台输入");
                pwd = console.readPassword("Password:");
            }
        }
 
        //不支持控制台输入,弹出gui输入
        if (StrUtils.isEmpty(pwd)) {
            Log.debug("无法从控制台中获取密码,GUI输入");
            InputForm input = new InputForm();
            boolean gui = input.showForm();
            if (gui) {
                Log.debug("GUI输入");
                pwd = input.nextPasswordLine();
                input.closeForm();
            }
        }
 
        //还是没有获取密码,退出
        if (StrUtils.isEmpty(pwd)) {
            Log.println("\nERROR: Startup failed, could not get the password.\n");
            System.exit(0);
        }
 
        //验证密码,jar包是才验证
        /*System.out.println("验证jar,密码路径"+JarUtils.getRootPath(null));
        byte[] passHash = JarDecryptor.readEncryptedFile2(new File(JarUtils.getRootPath(null)),Const.FILE_NAME2);
        if (passHash != null) {
            char[] p1 = StrUtils.merger(pwd, EncryptUtils.SALT);
            char[] p2 = EncryptUtils.md5(StrUtils.merger(pwd, EncryptUtils.SALT));
            p2 = EncryptUtils.md5(StrUtils.merger(EncryptUtils.SALT, p2));
            if (!StrUtils.equal(p1, p2)) {
                Log.println("\nERROR: Startup failed, invalid password.\n");
                System.exit(0);
            }
        }*/
        byte[] pwdHash = JarDecryptor.readEncryptedFile2(new File(JarUtils.getRootPath(null)),Const.FILE_NAME2);
        if (pwdHash != null) {
            String pwdTrue = EncryptUtils.deAES(new String(pwdHash, Charset.forName("utf-8")), Const.AES_KEY.toCharArray());
            pwdTrue = pwdTrue.replace(" ","").replace(",","").replace("[","").replace("]","");
            pwd = pwdTrue.toCharArray();
        }
 
        //GO
        if (inst != null) {
            AgentTransformer tran = new AgentTransformer(pwd);
            inst.addTransformer(tran);
        }
    }
 
    /**
     * 从文件读取密码
     *
     * @param options 参数开关
     * @return 密码
     */
    public static char[] readPasswordFromFile(CmdLineOption options) {
        String path = JarUtils.getRootPath(null);
        if (!path.endsWith(".jar")) {
            return null;
        }
        String jarName = path.substring(path.lastIndexOf("/") + 1);
        path = path.substring(0, path.lastIndexOf("/") + 1);
        String configName = jarName.substring(0, jarName.length() - 3) + "description.txt";
        File config = new File(path, configName);
        if (!config.exists()) {
            config = new File(path, "description.txt");
        }
 
        String args = null;
        if (config.exists()) {
            args = IoUtils.readTxtFile(config);
        }
 
        if (StrUtils.isEmpty(args)) {
            return null;
        }
 
        /*//不包含空格文件存的就是密码
        if (!args.contains(" ")) {
            return args.trim().toCharArray();
        }*/
        //perry
        //不包含空格文件存的就是密码
        if (args.contains(" ")) {
            return args.trim().replace(" ","").toCharArray();
        }
 
        options.parse(args.trim().split(" "));
        char[] pwd = options.getOptionValue("pwd", "").toCharArray();
        Const.DEBUG = options.hasOption("debug");
 
        //删除文件中的密码
        if (!"false".equalsIgnoreCase(options.getOptionValue("del"))
                && !"no".equalsIgnoreCase(options.getOptionValue("del"))) {
            args = "";
            IoUtils.writeTxtFile(config, args);
        }
        return pwd;
    }
 
    public static void main(String[] args) {
        char[] chars = JarDecryptor.readPassFromJar(new File(JarUtils.getRootPath(null)));
    }
}