对jar或者war进行加密解密
whycxzp
2021-07-15 cfdf00d3357268fa6de56db849e40aeaf714cff3
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
package com.whyc;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
 
/**
 * 密码输入界面,模拟命令行输入
 */
public class InputForm {
    //输入提示
    private static final String tips = " 项目正在启动,请输入启动密码\r\n Password: ";
    //密码显示字符
    private static final char passChar = '*';
    //窗口
    private JDialog frame;
    //文本域
    private JTextArea text;
    //已输入字符长度
    private int keyIndex = 0;
    //已输入的字符,最长100
    private char[] password = new char[100];
    //是否有下一行
    boolean hasNextLine = false;
 
    /**
     * 获取输入的密码
     *
     * @return 密码char数组
     */
    public char[] nextPasswordLine() {
        while (!hasNextLine) {
            try {
                Thread.sleep(50);
            } catch (Exception e) {
 
            }
        }
 
        int charsSize = 0;
        while (password[charsSize] != 0) {
            charsSize++;
        }
        char[] chars = new char[charsSize];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = password[i];
        }
 
        keyIndex = 0;
        password = new char[100];
        return chars;
 
    }
 
    /**
     * 显示窗口
     *
     * @return 显示是否成功
     */
    public boolean showForm() {
        try {
            frame = new JDialog();
            frame.setTitle("项目启动密码 - ClassFinal");
            frame.setSize(560, 320);
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setAlwaysOnTop(true);
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            text = new JTextArea();
            text.setFont(new Font(null, 0, 18));
            text.setBackground(new Color(0, 0, 0));
            text.setForeground(new Color(0, 255, 0));
            text.setText(tips);
            text.addKeyListener(getKeyAdapter());
            text.enableInputMethods(false);
            frame.add(text);
            frame.setVisible(true);
 
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
    /**
     * 关闭窗口
     */
    public void closeForm() {
        frame.setVisible(false);
        frame.dispose();
    }
 
    /**
     * 键盘监听事件
     *
     * @return KeyAdapter
     */
    private KeyAdapter getKeyAdapter() {
        return new KeyAdapter() {
            String fakePass = "";
 
            @Override
            public void keyReleased(KeyEvent e) {
                if (keyIndex < 100 && e.getKeyChar() > 32 && e.getKeyChar() < 127) {
                    password[keyIndex] = e.getKeyChar();
                    keyIndex++;
                    fakePass += passChar;
                } else if (keyIndex > 0 && e.getKeyCode() == 8) {//退格
                    keyIndex--;
                    password[keyIndex] = 0;
                    fakePass = fakePass.substring(1);
                } else if (e.getKeyCode() == 10) {//ENTER
                    fakePass = "";
                    hasNextLine = true;
                }
                text.setText(tips + fakePass);
            }
        };
    }
 
    public static void main(String[] args) {
        InputForm input = new InputForm();
        boolean gui = input.showForm();
        if (gui) {
            System.out.println(input.nextPasswordLine());
            input.closeForm();
        }
    }
}