AesUtil.java
3.49 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
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
package com.bckefu.uccc.aes;
import com.bckefu.uccc.CipherType;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* @Author caoliang1918@aliyun.com
*
* Date 2017/8/5 23:18
*
* 工作模式[CBC,CFB,ECB,OFB,PCBC]
* 填充方式[NoPadding/zero,PKCS5Padding , PKCS7Padding ,ISO10126Padding]
*
*/
public class AesUtil {
public static final String IV = "Pdt2WQ6vCU5MBY3n";
public static final String KEY = "a9VmT4PcXi7gFDkL";
/**
* 加密
* @param data
* @param password
* @param iv
* @return
*/
public static String encryptAES(String data, String password , String iv , String cipherType) {
if(StringUtils.isBlank(data)){
return null;
}
try {
Cipher cipher = Cipher.getInstance(cipherType);
SecretKeySpec secretKeySpec = new SecretKeySpec(password.getBytes(), CipherType.AES_ALGORITHM);
if(!cipherType.contains("ECB")){
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE , secretKeySpec, ivParameterSpec);
}else {
cipher.init(Cipher.ENCRYPT_MODE , secretKeySpec);
}
byte[] encrypted = cipher.doFinal(data.getBytes());
return Base64.encodeBase64String(encrypted);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
* @param decryptCode
* @param password
* @param iv
* @return
*/
public static String decryptAES(String decryptCode, String password , String iv , String cipherType) {
if(StringUtils.isBlank(decryptCode)){
return null;
}
try {
Cipher cipher = Cipher.getInstance(cipherType);
SecretKeySpec secretKeySpec = new SecretKeySpec(password.getBytes(), CipherType.AES_ALGORITHM);
if(!cipherType.contains("ECB")){
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE , secretKeySpec, ivParameterSpec);
}else {
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
}
byte[] encryptByte = Base64.decodeBase64(decryptCode);
byte[] original = cipher.doFinal(encryptByte);
return new String(original);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 加密
* @param data
* @param password
* @param iv
* @return
*/
public static String encryptAES(String data, String password , String iv){
return encryptAES(data ,password , iv , CipherType.AES_CBC_PKC5PADDING);
}
/**
* 解密
* @param decryptCode
* @param password
* @param iv
* @return
*/
public static String decryptAES(String decryptCode, String password , String iv) {
return decryptAES(decryptCode , password , iv , CipherType.AES_CBC_PKC5PADDING);
}
public static String encryptAES(String data, String password){
return encryptAES(data ,password , null , CipherType.AES_ECB_PKC5PADDING);
}
public static String decryptAES(String decryptCode, String password) {
return decryptAES(decryptCode , password , null , CipherType.AES_ECB_PKC5PADDING);
}
}