提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.util; |
E |
2 |
|
89ac7f
|
3 |
import java.util.*; |
5c5945
|
4 |
|
E |
5 |
/*** |
|
6 |
* �?单的加密程序,和解密程序 |
|
7 |
* |
|
8 |
* @author mq.c |
|
9 |
* |
|
10 |
*/ |
|
11 |
public class SimpleEncrypt { |
|
12 |
|
|
13 |
/** |
|
14 |
* 获取指定长度随机的字符串 |
|
15 |
* |
|
16 |
* @param getNum |
|
17 |
* @return |
|
18 |
*/ |
|
19 |
private static String generateCheckCode(Integer getNum) { |
|
20 |
// abcdefghijklmnopqrstuvwxyz |
|
21 |
// String chars = |
|
22 |
// "123456789.AB2CD3EF7G;HI5JK5LMN/OPQR8ST.UVW7X9YZ?~!@#$%^&*()_+{}:>?<|/.,';][=-"; |
|
23 |
// String chars = |
|
24 |
// "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+{}|:<>?/.,';][\\=-"; |
|
25 |
String chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789abcdefghijklmnopqrstuvwxyz._?"; |
|
26 |
String[] charsx = chars.split(""); |
|
27 |
// String[] charsx = chars.split(""); |
|
28 |
List<String> list = Arrays.asList(charsx); |
|
29 |
Collections.shuffle(list); |
|
30 |
StringBuffer buff = new StringBuffer(); |
|
31 |
Random random = new Random(); |
|
32 |
for (int i = 0; i < getNum; i++) { |
|
33 |
int rand = random.nextInt(list.size()); |
|
34 |
// buff.append(chars.charAt(rand)); |
|
35 |
buff.append(list.get(rand)); |
|
36 |
} |
|
37 |
return buff.toString(); |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* 对字符串进行简易加密(不要在意那种很容易被人破解的小细节) |
|
42 |
* 该加密采用"前面13位随机码"+dataString+"后9位随机码",组拼成一个字符串,进行BASE64加密得出的加密值 |
|
43 |
* |
|
44 |
* @param dataString |
|
45 |
* 要进行加密的数据源 |
|
46 |
* @return |
|
47 |
*/ |
|
48 |
public static String simpleEnCode(String dataString) { |
|
49 |
// 组拼随机值 |
|
50 |
String ranString = generateCheckCode(13).toString() + dataString + generateCheckCode(9).toString(); |
89ac7f
|
51 |
String enCode = new String(Base64.getEncoder().encode(ranString.getBytes())); |
5c5945
|
52 |
return enCode; |
E |
53 |
} |
|
54 |
|
|
55 |
/*** |
|
56 |
* 对用com.bang.units.SimpleEncrypt.simpleEnCode加密过的字符串进行解密 |
|
57 |
* |
|
58 |
* 该加密采用"前面13位随机码"+dataString+"后9位随机码",组拼成一个字符串,进行BASE64加密得出的加密值 |
|
59 |
* |
|
60 |
* @param dataString |
|
61 |
* 要进行解密的数据源 |
|
62 |
* @return |
|
63 |
*/ |
|
64 |
public static String simpleDeCode(String dataString) throws Exception { |
|
65 |
// 进行解密sun.misc.BASE64Decoder.BASE64Decoder() |
89ac7f
|
66 |
|
E |
67 |
byte[] enCode = Base64.getDecoder().decode(dataString); |
5c5945
|
68 |
// 解得随机值 |
E |
69 |
String ranString = new String(enCode); |
|
70 |
|
|
71 |
// System.out.println("simpleDeCode:" + ranString); |
|
72 |
// 截取出有效位置参数 |
|
73 |
String daString = ranString.substring(13, ranString.length() - 9); |
|
74 |
|
|
75 |
return daString; |
|
76 |
} |
|
77 |
|
|
78 |
public static void main(String[] args) throws Exception { |
|
79 |
String chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ?~!@#$%^&*()_+{}:>?<|/.,';][=-`"; |
|
80 |
String[] charsx = chars.split(""); |
|
81 |
List<String> list = Arrays.asList(charsx); |
|
82 |
Collections.shuffle(list); |
|
83 |
// for (String string : list) { |
|
84 |
// System.out.println(string); |
|
85 |
// } |
|
86 |
|
|
87 |
String str = "15648sadf"; |
|
88 |
String eStr = simpleEnCode(str); |
|
89 |
System.out.println(eStr); |
|
90 |
String dStr = simpleDeCode(eStr); |
|
91 |
System.out.println(dStr); |
|
92 |
|
|
93 |
} |
|
94 |
|
|
95 |
} |