/
AESUtils.java
128 lines (115 loc) · 4.42 KB
/
AESUtils.java
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
package org.hongxi.whatsmars.common.util;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
/**
* Created by shenhongxi on 15/5/27.
*/
public class AESUtils {
private static final String PADDING = "AES/ECB/PKCS5Padding";
private static final String DEFAULT_ENCODING = "utf-8";
/**
* 加密
*
* @param data
* 需要加密的内容
* @param key
* 加密密码
* @return
*/
public static byte[] encrypt(byte[] data, byte[] key) {
if(key.length!=16){
throw new RuntimeException("Invalid AES key length (must be 16 bytes)");
}
try {
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec seckey = new SecretKeySpec(enCodeFormat,"AES");
Cipher cipher = Cipher.getInstance(PADDING);// 创建密码器
cipher.init(Cipher.ENCRYPT_MODE, seckey);// 初始化
byte[] result = cipher.doFinal(data);
return result; // 加密
} catch (Exception e){
throw new RuntimeException("encrypt fail!", e);
}
}
/**
* 解密
*
* @param data
* 待解密内容
* @param key
* 解密密钥
* @return
*/
public static byte[] decrypt(byte[] data, byte[] key) {
if(key.length!=16){
throw new RuntimeException("Invalid AES key length (must be 16 bytes)");
}
try {
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance(PADDING);// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, seckey);// 初始化
byte[] result = cipher.doFinal(data);
return result; // 加密
} catch (Exception e){
throw new RuntimeException("decrypt fail!", e);
}
}
public static String encryptToBase64(String data, String key){
try {
byte[] valueByte = encrypt(data.getBytes(DEFAULT_ENCODING), key.getBytes(DEFAULT_ENCODING));
return new Base64().encodeToString(valueByte);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("encrypt fail!", e);
}
}
public static String decryptFromBase64(String data, String key){
try {
byte[] originalData = new Base64().decode(data.getBytes());
byte[] valueByte = decrypt(originalData, key.getBytes(DEFAULT_ENCODING));
return new String(valueByte, DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("decrypt fail!", e);
}
}
public static String encryptWithKeyBase64(String data, String key){
try {
byte[] valueByte = encrypt(data.getBytes(DEFAULT_ENCODING), new Base64().decode(key.getBytes()));
return new String(new Base64().encode(valueByte));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("encrypt fail!", e);
}
}
public static String decryptWithKeyBase64(String data, String key){
try {
byte[] originalData = new Base64().decode(data.getBytes());
byte[] valueByte = decrypt(originalData, new Base64().decode(key.getBytes()));
return new String(valueByte, DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("decrypt fail!", e);
}
}
public static byte[] genarateRandomKey(){
KeyGenerator keygen = null;
try {
keygen = KeyGenerator.getInstance(PADDING);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(" genarateRandomKey fail!", e);
}
SecureRandom random = new SecureRandom();
keygen.init(random);
Key key = keygen.generateKey();
return key.getEncoded();
}
public static String genarateRandomKeyWithBase64(){
return new String(new Base64().encode(genarateRandomKey()));
}
}