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
package cn.wisenergy.chnmuseum.party.auth.util;
import org.apache.commons.lang3.StringUtils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class AESUtils {
private static String KEY = "guobomimajiamics";
private static String IV = "guobomimajiamics";
/**
* AES解密
* @param encryptStr 密文
* @return 明文
* @throws Exception
*/
public static String aesDecrypt(String encryptStr) throws Exception {
if (StringUtils.isEmpty(encryptStr)) {
return null;
}
byte[] encryptByte = Base64.getDecoder().decode(encryptStr);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY.getBytes(), "AES"),new IvParameterSpec(IV.getBytes()));
byte[] decryptBytes = cipher.doFinal(encryptByte);
return new String(decryptBytes);
}
/**
* AES加密
* @param content 明文
* @return 密文
* @throws Exception
*/
public static String aesEncrypt(String content) throws Exception {
if (StringUtils.isEmpty(content)) {
return null;
}
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY.getBytes(), "AES"),new IvParameterSpec(IV.getBytes()));
byte[] encryptStr = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptStr);
}
}