-
Notifications
You must be signed in to change notification settings - Fork 802
/
AESUtils.java
132 lines (103 loc) · 3.84 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
129
130
131
132
package com.jingewenku.abrahamcaijin.commonutil.encryption;
import android.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @Description:主要功能:AES对称加密(Advanced Encryption Standard,高级数据加密标准,AES算法可以有效抵制针对DES的攻击算法,对称加密算法)
* @Prject: CommonUtilLibrary
* @Package: com.jingewenku.abrahamcaijin.commonutil.encryption
* @author: AbrahamCaiJin
* @date: 2017年05月16日 15:54
* @Copyright: 个人版权所有
* @Company:
* @version: 1.0.0
*/
public class AESUtils {
/*
* 生成密钥
*/
public static byte[] initKey() throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256); //192 256
SecretKey secretKey = keyGen.generateKey();
return secretKey.getEncoded();
}
/*
* AES 加密
*/
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherBytes = cipher.doFinal(data);
return cipherBytes;
}
/*
* AES 解密
*/
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] plainBytes = cipher.doFinal(data);
return plainBytes;
}
private SecretKeySpec skeySpec;
private Cipher cipher;
public AESUtils(byte[] keyraw) throws Exception {
if (keyraw == null) {
byte[] bytesOfMessage = "".getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(bytesOfMessage);
skeySpec = new SecretKeySpec(bytes, "AES");
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
} else {
skeySpec = new SecretKeySpec(keyraw, "AES");
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
}
}
public AESUtils(String passphrase) throws Exception {
byte[] bytesOfMessage = passphrase.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
skeySpec = new SecretKeySpec(thedigest, "AES");
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
}
public AESUtils() throws Exception {
byte[] bytesOfMessage = "".getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
skeySpec = new SecretKeySpec(thedigest, "AES");
skeySpec = new SecretKeySpec(new byte[16], "AES");
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
}
/**
* 加密:先AES再base64
*
* @param plaintext
* @return
* @throws Exception
*/
public String encrypt(String plaintext) throws Exception {
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] ciphertext = cipher.doFinal(plaintext.getBytes("UTF-8"));
return Base64.encodeToString(ciphertext, Base64.DEFAULT);
}
/**
* 解密:先base64再AES
*
* @param ciphertext
* @return
* @throws Exception
*/
public String decrypt(String ciphertext) throws Exception {
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] plain64text = Base64.decode(ciphertext, Base64.DEFAULT);
byte[] plaintext = cipher.doFinal(plain64text);
return new String(plaintext, "UTF-8");
}
}