Skip to content

Commit d9f9fb4

Browse files
author
xqs
committed
bugfix
1 parent c90ea61 commit d9f9fb4

File tree

3 files changed

+88
-30
lines changed

3 files changed

+88
-30
lines changed

src/main/java/com/devil/utils/AES128Coder.java

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import java.security.NoSuchAlgorithmException;
66
import java.security.SecureRandom;
77
import java.util.Arrays;
8+
import java.util.Base64;
89

910
import javax.crypto.Cipher;
1011
import javax.crypto.KeyGenerator;
1112
import javax.crypto.SecretKey;
13+
import javax.crypto.spec.IvParameterSpec;
1214
import javax.crypto.spec.SecretKeySpec;
1315

1416
/*
@@ -19,9 +21,14 @@
1921

2022
public class AES128Coder {
2123
private static final String KEY_TYPE = "AES";
22-
private static final String CIPHER_ALGORITHM_ECB = "AES/ECB/PKCS5Padding";
24+
private static final String AES = "AES";
25+
private static final String AES_ECB = "AES/ECB/PKCS5Padding";
26+
private static final String AES_CBC = "AES/CBC/PKCS5Padding";
27+
private static final String ALGORITHM = AES_ECB;
2328

24-
// private static final String CIPHER_ALGORITHM_ECB = "AES";
29+
// "AES/ECB/PKCS5Padding";
30+
31+
// private static final String CIPHER_ALGORITHM_ECB = "AES";
2532

2633
private byte[] key = null;
2734

@@ -46,15 +53,31 @@ public AES128Coder(byte[] key) {
4653
} else if (key.length > 16) {
4754
key = Arrays.copyOf(key, 16);
4855
}
49-
System.out.println("key="+Arrays.toString(key));
56+
System.out.println("key=" + Arrays.toString(key));
5057
this.key = key;
5158
}
5259

5360
public static void main(String[] args) {
61+
// System.err.println("providers:" +
62+
// Arrays.toString(Security.getProviders()));
63+
// System.err.println("Cipher:" +
64+
// DebugUtil.sortToString(Security.getAlgorithms("Cipher")));
65+
// System.err.println("Signature:" +
66+
// DebugUtil.sortToString(Security.getAlgorithms("Signature")));
67+
// System.err.println("MessageDigest:" +
68+
// DebugUtil.sortToString(Security.getAlgorithms("MessageDigest")));
69+
// System.err.println("KeyStore:" +
70+
// DebugUtil.sortToString(Security.getAlgorithms("KeyStore")));
71+
// System.err.println("Mac:" +
72+
// DebugUtil.sortToString(Security.getAlgorithms("Mac")));
73+
// Provider jceProvider = Security.getProvider("SunJCE");
74+
// System.err.println("JCE::" +
75+
// DebugUtil.sortToString(jceProvider.getServices()));
76+
System.out.println("======================================");
5477
// String src = "guessyourheart";
5578
// System.out.println("原文是:" + src);
5679
// byte[] key = "guessyourheart".getBytes(Charset.forName("UTF-8"));
57-
String src = "123455";
80+
String src = "1234567812345678";
5881
System.out.println("原文是:" + src);
5982
byte[] key = "12345678".getBytes(Charset.forName("UTF-8"));
6083
AES128Coder coder = new AES128Coder(key);
@@ -74,13 +97,12 @@ public static void main(String[] args) {
7497
}
7598

7699
public String encrypt(byte[] src) {
77-
byte[] bytes = this.aesEncrypt(src);
78-
System.out.println("bytes[]=" + Arrays.toString(bytes));
79-
return CommUtil.encBase64Url(bytes);
100+
// System.out.println("bytes[]=" + ByteUtil.toHexString(bytes));
101+
return Base64.getMimeEncoder().encodeToString(this.aesEncrypt(src));
80102
}
81103

82104
public byte[] decrypt(String src) {
83-
return this.aesDecrypt(CommUtil.decBase64Url(src));
105+
return this.aesDecrypt(Base64.getMimeDecoder().decode(src));
84106
}
85107

86108
/********** public functions ends ******************/
@@ -110,31 +132,56 @@ private Key toKey(byte[] key) throws Exception {
110132
return new SecretKeySpec(key, KEY_TYPE);
111133
}
112134

113-
private byte[] aesDecrypt(byte[] cipherText) {
135+
private byte[] aesDecrypt(byte[] cipherBytes) {
114136
byte[] key = this.key;
115137
byte[] plainText = null;
116138
try {
117139
Key k = this.toKey(key);
118-
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
119-
cipher.init(Cipher.DECRYPT_MODE, k);
120-
plainText = cipher.doFinal(cipherText);
140+
// System.out.println(
141+
// "decrypt key:" + k.getAlgorithm() + "--" + k.getFormat() + "--" +
142+
// Arrays.toString(k.getEncoded()));
143+
Cipher cipher = Cipher.getInstance(ALGORITHM);
144+
if (AES_CBC.equals(ALGORITHM)) {
145+
IvParameterSpec iv = new IvParameterSpec(new byte[16]);// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
146+
cipher.init(Cipher.DECRYPT_MODE, k, iv);
147+
} else {
148+
cipher.init(Cipher.DECRYPT_MODE, k);
149+
}
150+
// System.out.println("cipher:" + cipher.getAlgorithm() +
151+
// "--blockSize:" + cipher.getBlockSize()
152+
// + "--outputsize:" + cipher.getOutputSize(cipherBytes.length) +
153+
// "--provider:" + cipher.getProvider());
154+
plainText = cipher.doFinal(cipherBytes);
121155
} catch (Exception e) {
122-
e.printStackTrace();
156+
throw new IllegalStateException(e);
123157
}
124158

125159
return plainText;
126160
}
127161

128-
private byte[] aesEncrypt(byte[] plainText) {
162+
private byte[] aesEncrypt(byte[] plainBytes) {
129163
byte[] key = this.key;
130164
byte[] cipherText = null;
131165
try {
132166
Key k = this.toKey(key);
133-
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
134-
cipher.init(Cipher.ENCRYPT_MODE, k);
135-
cipherText = cipher.doFinal(plainText);
167+
// System.out.println(
168+
// "encrypt key:" + k.getAlgorithm() + "--" + k.getFormat() + "--" +
169+
// Arrays.toString(k.getEncoded()));
170+
Cipher cipher = Cipher.getInstance(ALGORITHM);
171+
172+
if (AES_CBC.equals(ALGORITHM)) {
173+
IvParameterSpec iv = new IvParameterSpec(new byte[16]);// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
174+
cipher.init(Cipher.ENCRYPT_MODE, k, iv);
175+
} else {
176+
cipher.init(Cipher.ENCRYPT_MODE, k);
177+
}
178+
// System.out.println("cipher:" + cipher.getAlgorithm() +
179+
// "--blockSize:" + cipher.getBlockSize()
180+
// + "--outputsize:" + cipher.getOutputSize(plainBytes.length) +
181+
// "--provider:" + cipher.getProvider());
182+
cipherText = cipher.doFinal(plainBytes);
136183
} catch (Exception e) {
137-
e.printStackTrace();
184+
throw new IllegalStateException(e);
138185
}
139186

140187
return cipherText;

src/main/java/com/devil/utils/CommUtil.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ public static <T> boolean eqAny(T target, T... any) {
5252
}
5353
return false;
5454
}
55-
public static boolean isSubClass(Class<?> clz,Class<?> superclz) {
55+
56+
public static boolean isSubClass(Class<?> clz, Class<?> superclz) {
5657
try {
58+
/**
59+
* 正好与 isAssignableFrom相反
60+
*/
5761
clz.asSubclass(superclz);
5862
return true;
5963
} catch (ClassCastException e) {
@@ -143,7 +147,7 @@ public static String fmtException(Throwable e) {
143147
public static <T extends Throwable> T getException(Throwable e, Class<T> type) {
144148
while (true) {
145149
if (isSubClass(type, e.getClass())) {
146-
return (T)e;
150+
return (T) e;
147151
} else {
148152
e = e.getCause();
149153
if (e == null) {
@@ -162,7 +166,7 @@ public static Throwable getException(Throwable e, String clzName) {
162166
throw new IllegalArgumentException(clzName);
163167
}
164168
}
165-
169+
166170
public static int randInt(int len) {
167171
if (len > 9) {
168172
throw new IllegalArgumentException("the length of 'len' must be less than 9");
@@ -186,22 +190,24 @@ public static String randString(int len) {
186190
}
187191
return new String(dest);
188192
}
193+
189194
public static String randLetter(int len) {
190195
char[] dest = new char[len];
191196
Random random = new Random();
192197
for (int i = 0; i < len; i++) {
193198
int r = random.nextInt(26);
194-
dest[i] = alpha[r+10];
199+
dest[i] = alpha[r + 10];
195200
}
196201
return new String(dest);
197202
}
203+
198204
public static String randNum(int len) {
199205
char[] dest = new char[len];
200206
Random random = new Random();
201-
dest[0]=alpha[random.nextInt(9)+1];
207+
dest[0] = alpha[random.nextInt(9) + 1];
202208
for (int i = 1; i < len; i++) {
203209
int r = random.nextInt(10);
204-
dest[i] = alpha[r+10];
210+
dest[i] = alpha[r + 10];
205211
}
206212
return new String(dest);
207213
}

src/main/java/com/devil/utils/DebugUtil.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@
44
import java.util.Arrays;
55
import java.util.Collection;
66
import java.util.Collections;
7+
import java.util.Iterator;
78
import java.util.List;
89

910
import javax.swing.JComponent;
1011
import javax.swing.JFrame;
1112
import javax.swing.WindowConstants;
1213

1314
public final class DebugUtil {
14-
public static <T extends Comparable<? super T>> List<T> sort(Collection<T> collection) {
15-
List<T> list = new ArrayList<>(collection);
15+
public static String sortToString(Collection<?> collection) {
16+
List<String> list = new ArrayList<>(collection.size());
17+
Iterator<?> it = collection.iterator();
18+
while (it.hasNext()) {
19+
list.add(it.next().toString());
20+
}
1621
Collections.sort(list);
17-
return list;
22+
return list.toString();
1823
}
1924

20-
public static <T extends Comparable<? super T>> T[] sort(T... arr) {
25+
public static String sortToString(Object... arr) {
2126
Arrays.sort(arr);
22-
return arr;
27+
return Arrays.toString(arr);
2328
}
2429

25-
public static <T> void printArr(T[] arr) {
30+
public static <T> void printAsLine(T[] arr) {
2631
if (arr != null) {
2732
for (T t : arr) {
2833
System.out.println(t);

0 commit comments

Comments
 (0)