AESUtil.java
4.23 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package cn.com.glsx.consumer.modules.utils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;
/**
* AES PKCS5
*/
public class AESUtil {
private static final String ENCRYPT_AES = "AES/CBC/PKCS5Padding";
public static final String CHARSET_UTF_8 = "utf-8";
private static final int SECURE_KEY_LENGTH = 16;
/**
* AES ECB 128加密
* iv偏移量,ECB不用设置
* 加密结果再进行Base64加密
* @param src
* @param key
* @throws Exception
*/
public static String encryptEcb(String src, byte[] key) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] content = src.getBytes(CHARSET_UTF_8);
byte[] ret = cipher.doFinal(content);
return Base64.getEncoder().encodeToString(ret);
}
/**
* ECB解密 结果含Base64加密
* @param src
* @param key
* @return
* @throws Exception
*/
public static String decryptEcb(String src,byte[] key) throws Exception{
byte[] srcByte = Base64.getDecoder().decode(src);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] ret = cipher.doFinal(srcByte);
return new String(ret,CHARSET_UTF_8);
}
/**
* AES CBC 128加密
* 加密结果再进行Base64编码
* 动态偏移量
* @param src 加密字符串
* @param key 密码 16位
* @throws Exception
*/
public static String encryptCbc(String src, byte[] key) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
AlgorithmParameterSpec params = new IvParameterSpec(getIv(key));
Cipher cipher = Cipher.getInstance(ENCRYPT_AES);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, params);
byte[] content = src.getBytes(CHARSET_UTF_8);
byte[] ret = cipher.doFinal(content);
return Base64.getEncoder().encodeToString(ret);
}
/**
* AES CBC 解密
* 结果含Base64编码
* 动态偏移量
* @param src 加密字符串
* @param key 密码 16位
* @return
* @throws Exception
*/
public static String decryptCbc(String src,byte[] key) throws Exception{
byte[] srcByte = Base64.getDecoder().decode(src);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
AlgorithmParameterSpec params = new IvParameterSpec(getIv(key));
Cipher cipher = Cipher.getInstance(ENCRYPT_AES);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, params);
byte[] ret = cipher.doFinal(srcByte);
return new String(ret,CHARSET_UTF_8);
}
/**
* AES 128获取16位密码
* @param key
* @return
* @throws UnsupportedEncodingException
*/
public static byte[] getAESKey(String key) throws UnsupportedEncodingException {
byte[] keyBytes = key.getBytes(CHARSET_UTF_8);
// Use the first 16 bytes (or even less if key is shorter)
byte[] keyBytes128 = new byte[SECURE_KEY_LENGTH];
System.arraycopy(keyBytes, 0, keyBytes128, 0, Math.min(keyBytes.length, SECURE_KEY_LENGTH));
return keyBytes128;
}
/**
* 生成IV偏移量
* @param key
* @return
*/
public static byte[] getIv(byte[] key){
byte[] iv = new byte[SECURE_KEY_LENGTH];
for(int i=0; i< SECURE_KEY_LENGTH && i < key.length; i++){
iv[i] = key[i];
}
if(key.length < SECURE_KEY_LENGTH){
for(int i=key.length; i<SECURE_KEY_LENGTH; i++){
iv[i] = '0';
}
}
return iv;
}
public static void main(String[] args)throws Exception{
String aa="{\"did\":\"lumi.158d0001b12fdb\",\"msgId\":\"AC1F000E676355F9630284AC6BD36AF93\",\"property\":\"system_volume\",\"time\":\"1561544292194\",\"value\":\"40\"}";
System.out.println(encryptEcb(aa,"H1rhtzCNw8HGS5zwCDaLf4XJl0Tsvijg".getBytes()));
}
}