-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCryptAES.java
More file actions
87 lines (79 loc) · 2.58 KB
/
CryptAES.java
File metadata and controls
87 lines (79 loc) · 2.58 KB
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
package com.huangpugroup.services.tools;
import com.huangpugroup.services.Comm;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.SecretKeySpec;
//依懒commons-codec-1.7
import org.apache.commons.codec.binary.Base64;
/**
* 兼容PHP的AES加密类
*
* @author 郭冰冰
*/
public class CryptAES {
private static final String AESTYPE = "AES/ECB/PKCS5Padding";
private static final Class OBJ = CryptAES.class;
/**
* AES加密
*
* @param keyStr 密钥
* @param plainText 待加密字符串
* @return 加密后的字符串
*/
public static String AES_Encrypt(String keyStr, String plainText) {
byte[] encrypt = null;
try {
Key key = generateKey(keyStr);
Cipher cipher = Cipher.getInstance(AESTYPE);
cipher.init(Cipher.ENCRYPT_MODE, key);
encrypt = cipher.doFinal(plainText.getBytes());
} catch (Exception e) {
Comm.throwErrorLog(OBJ, "AES加密失败!", e);
}
return new String(Base64.encodeBase64(encrypt));
}
/**
* AES解密
*
* @param keyStr 密钥
* @param encryptData 待解密字符串
* @return 解密后的字符串
*/
public static String AES_Decrypt(String keyStr, String encryptData) throws IllegalBlockSizeException {
byte[] decrypt = null;
try {
Key key = generateKey(keyStr);
Cipher cipher = Cipher.getInstance(AESTYPE);
cipher.init(Cipher.DECRYPT_MODE, key);
decrypt = cipher.doFinal(Base64.decodeBase64(encryptData));
} catch (Exception e) {
Comm.throwErrorLog(OBJ, "AES解密失败!", e);
}
return new String(decrypt).trim();
}
/**
* 构造一个密钥
*
* @param key 密钥
* @return
* @throws Exception
*/
private static Key generateKey(String key) throws Exception {
try {
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
return keySpec;
} catch (Exception e) {
Comm.throwErrorLog(OBJ, "AES构造密钥失败!", e);
throw e;
}
}
public static void main(String[] args) throws IllegalBlockSizeException {
String keyStr = "cly123zhang58s@d";
String plainText = "我真的是一个好人";
String encText = AES_Encrypt(keyStr, plainText);
String decString = AES_Decrypt(keyStr, encText);
System.out.println(encText);
System.out.println(decString);
}
}