35 | * It may be specified as requiring an argument. 36 | *
37 | * 38 | * @param opt Short single-character name of the option. 39 | * @param hasArg flag signally if an argument is required after this option 40 | * @param description Self-documenting description 41 | * @return the resulting Options instance 42 | */ 43 | public CmdLineOption addOption(String opt, boolean hasArg, String description) { 44 | opt = resolveOption(opt); 45 | if (!options.contains(opt)) { 46 | options.add(opt); 47 | hasArgs.add(hasArg); 48 | descriptions.add(description); 49 | } 50 | return this; 51 | } 52 | 53 | /** 54 | * Parse the arguments according to the specified options. 55 | * 56 | * @param arguments the command line arguments 57 | * @return CmdLineOption 58 | */ 59 | public CmdLineOption parse(String[] arguments) { 60 | int optIndex = -1; 61 | for (int i = 0; i < arguments.length; i++) { 62 | String arg = arguments[i]; 63 | 64 | if (arg.startsWith("-") || arg.startsWith("--")) { 65 | arg = resolveOption(arg); 66 | 67 | //check last option hasArg 68 | if (optIndex > -1 && hasArgs.get(optIndex)) { 69 | String lastOption = options.get(optIndex); 70 | if (optionsMap.get(lastOption).size() == 0) { 71 | throw new IllegalArgumentException("Missing argument for option: " + lastOption); 72 | } 73 | } 74 | 75 | optIndex = options.indexOf(arg); 76 | if (optIndex < 0) { 77 | throw new IllegalArgumentException("Unrecognized option: " + arguments[i]); 78 | } 79 | optionsMap.put(arg, new ArrayList<>()); 80 | } else if (optIndex > -1) { 81 | String option = options.get(optIndex); 82 | optionsMap.get(option).add(arg); 83 | } 84 | } 85 | return this; 86 | } 87 | 88 | /** 89 | * Retrieve the first argument, if any, of this option. 90 | * 91 | * @param opt the name of the option 92 | * @return Value of the argument if option is set, and has an argument, 93 | * otherwise null. 94 | */ 95 | public String getOptionValue(String opt) { 96 | return getOptionValue(opt, null); 97 | } 98 | 99 | /** 100 | * Retrieve the first argument, if any, of this option. 101 | * 102 | * @param opt the name of the option 103 | * @param dv default value 104 | * @return Value of the argument if option is set, and has an argument, 105 | * otherwise null. 106 | */ 107 | public String getOptionValue(String opt, String dv) { 108 | String[] values = getOptionValues(opt); 109 | return (values == null) ? dv : values[0]; 110 | } 111 | 112 | /** 113 | * Retrieves the array of values, if any, of an option. 114 | * 115 | * @param opt string name of the option 116 | * @return Values of the argument if option is set, and has an argument, 117 | * otherwise null. 118 | */ 119 | public String[] getOptionValues(String opt) { 120 | Liststr
and
136 | * return the new String.
137 | *
138 | * @param str The string from which the hyphens should be removed.
139 | * @return the new String.
140 | */
141 | private static String resolveOption(String str) {
142 | if (str == null) {
143 | return null;
144 | }
145 | if (str.startsWith("--")) {
146 | return str.substring(2, str.length());
147 | } else if (str.startsWith("-")) {
148 | return str.substring(1, str.length());
149 | }
150 |
151 | return str;
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/classfinal-core/src/main/java/net/roseboy/classfinal/util/EncryptUtils.java:
--------------------------------------------------------------------------------
1 | package net.roseboy.classfinal.util;
2 |
3 | import javax.crypto.Cipher;
4 | import javax.crypto.spec.SecretKeySpec;
5 | import java.math.BigInteger;
6 | import java.security.*;
7 | import java.security.interfaces.RSAPrivateKey;
8 | import java.security.interfaces.RSAPublicKey;
9 | import java.security.spec.PKCS8EncodedKeySpec;
10 | import java.security.spec.X509EncodedKeySpec;
11 | import java.util.*;
12 |
13 | /**
14 | * 简单加密解密
15 | *
16 | * @author roseboy
17 | */
18 | public class EncryptUtils {
19 | //盐
20 | public static final char[] SALT = {'w', 'h', 'o', 'i', 's', 'y', 'o', 'u', 'r', 'd', 'a', 'd', 'd', 'y', '#', '$', '@', '#', '@'};
21 | //rsa 长度
22 | private static int KEY_LENGTH = 1024;
23 |
24 | /**
25 | * 加密
26 | *
27 | * @param msg 内容
28 | * @param key 密钥
29 | * @param type 类型
30 | * @return 密文
31 | */
32 | public static byte[] en(byte[] msg, char[] key, int type) {
33 | if (type == 1) {
34 | return enAES(msg, md5(StrUtils.merger(key, SALT), true));
35 | }
36 | return enSimple(msg, key);
37 | }
38 |
39 | /**
40 | * 解密
41 | *
42 | * @param msg 密文
43 | * @param key 密钥
44 | * @param type 类型
45 | * @return 明文
46 | */
47 | public static byte[] de(byte[] msg, char[] key, int type) {
48 | if (type == 1) {
49 | return deAES(msg, md5(StrUtils.merger(key, SALT), true));
50 | }
51 | return deSimple(msg, key);
52 | }
53 |
54 | /**
55 | * md5加密
56 | *
57 | * @param str 字符串
58 | * @return md5字串
59 | */
60 | public static byte[] md5byte(char[] str) {
61 | byte[] b = null;
62 | try {
63 | MessageDigest md = MessageDigest.getInstance("MD5");
64 | byte[] buffer = StrUtils.toBytes(str);
65 | md.update(buffer);
66 | b = md.digest();
67 | } catch (NoSuchAlgorithmException e) {
68 | e.printStackTrace();
69 | }
70 | return b;
71 | }
72 |
73 | /**
74 | * md5
75 | *
76 | * @param str 字串
77 | * @return 32位md5
78 | */
79 | public static char[] md5(char[] str) {
80 | return md5(str, false);
81 | }
82 |
83 | /**
84 | * md5
85 | *
86 | * @param str 字串
87 | * @param sh0rt 是否16位
88 | * @return 32位/16位md5
89 | */
90 | public static char[] md5(char[] str, boolean sh0rt) {
91 | byte s[] = md5byte(str);
92 | if (s == null) {
93 | return null;
94 | }
95 | int begin = 0;
96 | int end = s.length;
97 | if (sh0rt) {
98 | begin = 8;
99 | end = 16;
100 | }
101 | char[] result = new char[0];
102 | for (int i = begin; i < end; i++) {
103 | result = StrUtils.merger(result, Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6).toCharArray());
104 | }
105 | return result;
106 | }
107 |
108 |
109 | /**
110 | * 加密
111 | *
112 | * @param msg 加密报文
113 | * @param start 开始位置
114 | * @param end 结束位置
115 | * @param key 密钥
116 | * @return 加密后的字节
117 | */
118 | public static byte[] enSimple(byte[] msg, int start, int end, char[] key) {
119 | byte[] keys = IoUtils.merger(md5byte(StrUtils.merger(key, SALT)), md5byte(StrUtils.merger(SALT, key)));
120 | for (int i = start; i <= end; i++) {
121 | msg[i] = (byte) (msg[i] ^ keys[i % keys.length]);
122 | }
123 | return msg;
124 | }
125 |
126 | /**
127 | * 解密
128 | *
129 | * @param msg 加密报文
130 | * @param start 开始位置
131 | * @param end 结束位置
132 | * @param key 密钥
133 | * @return 解密后的字节
134 | */
135 | public static byte[] deSimple(byte[] msg, int start, int end, char[] key) {
136 | byte[] keys = IoUtils.merger(md5byte(StrUtils.merger(key, SALT)), md5byte(StrUtils.merger(SALT, key)));
137 | for (int i = start; i <= end; i++) {
138 | msg[i] = (byte) (msg[i] ^ keys[i % keys.length]);
139 | }
140 | return msg;
141 | }
142 |
143 | /**
144 | * 加密
145 | *
146 | * @param msg 加密报文
147 | * @param key 密钥
148 | * @return 加密后的字节
149 | */
150 | public static byte[] enSimple(byte[] msg, char[] key) {
151 | return enSimple(msg, 0, msg.length - 1, key);
152 | }
153 |
154 | /**
155 | * 解密
156 | *
157 | * @param msg 加密报文
158 | * @param key 密钥
159 | * @return 解密后的字节
160 | */
161 | public static byte[] deSimple(byte[] msg, char[] key) {
162 | return deSimple(msg, 0, msg.length - 1, key);
163 | }
164 |
165 | /**
166 | * RSA公钥加密
167 | *
168 | * @param str 加密字符串
169 | * @param publicKey 公钥
170 | * @return 密文
171 | */
172 | public static String enRSA(String str, String publicKey) {
173 | try {
174 | byte[] in = str.getBytes("UTF-8");
175 | byte[] out = enRSA(in, publicKey);
176 | String outStr = Base64.getEncoder().encodeToString(out);
177 | return outStr;
178 | } catch (Exception e) {
179 | e.printStackTrace();
180 | }
181 | return null;
182 | }
183 |
184 | /**
185 | * RSA公钥加密
186 | *
187 | * @param msg 要加密的字节
188 | * @param publicKey 公钥
189 | * @return 解密后的字节
190 | */
191 | public static byte[] enRSA(byte[] msg, String publicKey) {
192 | try {
193 | //base64编码的公钥
194 | byte[] decoded = Base64.getDecoder().decode(publicKey.getBytes("UTF-8"));
195 | RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
196 | //RSA加密
197 | Cipher cipher = Cipher.getInstance("RSA");
198 | cipher.init(Cipher.ENCRYPT_MODE, pubKey);
199 | return cipherDoFinal(cipher, msg, Cipher.ENCRYPT_MODE);
200 |
201 | } catch (Exception e) {
202 | e.printStackTrace();
203 | }
204 | return null;
205 | }
206 |
207 |
208 | /**
209 | * RSA私钥解密
210 | *
211 | * @param str 要解密的字符串
212 | * @param privateKey 私钥
213 | * @return 明文
214 | */
215 | public static String deRSA(String str, String privateKey) {
216 | try {
217 | //64位解码加密后的字符串
218 | byte[] inputByte = Base64.getDecoder().decode(str.getBytes("UTF-8"));
219 | String outStr = new String(deRSA(inputByte, privateKey));
220 | return outStr;
221 | } catch (Exception e) {
222 | e.printStackTrace();
223 | }
224 | return null;
225 | }
226 |
227 | /**
228 | * RSA私钥解密
229 | *
230 | * @param msg 要解密的字节
231 | * @param privateKey 私钥
232 | * @return 解密后的字节
233 | */
234 | public static byte[] deRSA(byte[] msg, String privateKey) {
235 | try {
236 | //base64编码的私钥
237 | byte[] decoded = Base64.getDecoder().decode(privateKey.getBytes("UTF-8"));
238 | RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
239 | //RSA解密
240 | Cipher cipher = Cipher.getInstance("RSA");
241 | cipher.init(Cipher.DECRYPT_MODE, priKey);
242 | return cipherDoFinal(cipher, msg, Cipher.DECRYPT_MODE);
243 |
244 | } catch (Exception e) {
245 | e.printStackTrace();
246 | }
247 | return null;
248 | }
249 |
250 | /**
251 | * 调用加密解密
252 | *
253 | * @param cipher Cipher
254 | * @param msg 要加密的字节
255 | * @param mode 解密/解密
256 | * @return 结果
257 | * @throws Exception Exception
258 | */
259 | private static byte[] cipherDoFinal(Cipher cipher, byte[] msg, int mode) throws Exception {
260 | int in_length = 0;
261 | if (mode == Cipher.ENCRYPT_MODE) {
262 | in_length = KEY_LENGTH / 8 - 11;
263 | } else if (mode == Cipher.DECRYPT_MODE) {
264 | in_length = KEY_LENGTH / 8;
265 | }
266 |
267 | byte[] in = new byte[in_length];
268 | byte[] out = new byte[0];
269 |
270 | for (int i = 0; i < msg.length; i++) {
271 | if (msg.length - i < in_length && i % in_length == 0) {
272 | in = new byte[msg.length - i];
273 | }
274 | in[i % in_length] = msg[i];
275 | if (i == (msg.length - 1) || (i % in_length + 1 == in_length)) {
276 | out = IoUtils.merger(out, cipher.doFinal(in));
277 | }
278 | }
279 | return out;
280 | }
281 |
282 | /**
283 | * 生成密钥对
284 | *
285 | * @return 密钥信息
286 | * @throws NoSuchAlgorithmException NoSuchAlgorithmException
287 | */
288 | public static Map