-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathEncryptAndDecrypt.java
More file actions
125 lines (112 loc) · 5.05 KB
/
EncryptAndDecrypt.java
File metadata and controls
125 lines (112 loc) · 5.05 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
package cn.aofeng.demo.encrypt;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* 加密与解密。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class EncryptAndDecrypt {
public final static String CHARSET = "utf8";
/**
* 创建安全密钥。
*
* @param encryptType
* 加密方式,如:AES,Blowfish。详情查看<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher">Java Cryptography Architecture Standard Algorithm Name Documentation</a>
* @param keyStr
* 密钥明文
* @return 安全密钥
* @throws UnsupportedEncodingException
* 不支持指定的字符集编码
*/
public SecretKey createSecretKey(String encryptType, String keyStr)
throws UnsupportedEncodingException {
byte[] secretKeyData = keyStr.getBytes(CHARSET);
SecretKeySpec sks = new SecretKeySpec(secretKeyData, encryptType);
return sks;
}
/**
* 加密数据。
*
* @param encryptType 加密方式,如:AES,Blowfish。详情查看<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher">Java Cryptography Architecture Standard Algorithm Name Documentation</a>
* @param secretKey 密钥
* @param srcData 待加密的源数据
* @return 加密后的二进制数据(字节数组)
* @see #encrypt(String, SecretKey, String, String)
*/
public byte[] encrypt(String encryptType, SecretKey secretKey,
String srcData) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
return encrypt(encryptType, secretKey, srcData, null);
}
/**
* 加密数据。
*
* @param encryptType 加密类型,如:AES/CBC/PKCS5Padding
* @param secretKey 密钥
* @param srcData 待加密的源数据
* @param algorithmParam 某些加密算法的附加参数
* @return 加密后的二进制数据(字节数组)
*/
public byte[] encrypt(String encryptType, SecretKey secretKey,
String srcData, String algorithmParam) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
Cipher encrpyt = Cipher.getInstance(encryptType);
if (null == algorithmParam) {
encrpyt.init(Cipher.ENCRYPT_MODE, secretKey);
} else {
IvParameterSpec iv = new IvParameterSpec(
algorithmParam.getBytes(CHARSET));
encrpyt.init(Cipher.ENCRYPT_MODE, secretKey, iv);
}
byte[] secretData = encrpyt.doFinal(srcData.getBytes(CHARSET));
return secretData;
}
/**
* 解密数据。
*
* @param decryptType 解密方式,如:AES,Blowfish。详情查看<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher">Java Cryptography Architecture Standard Algorithm Name Documentation</a>
* @param secretKey 密钥
* @param secretData 待解密的数据
* @return 解密后的数据
* @see #decrypt(String, SecretKey, byte[], String)
*/
public String decrypt(String decryptType, SecretKey secretKey,
byte[] secretData) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
return decrypt(decryptType, secretKey, secretData, null);
}
public String decrypt(String decryptType, SecretKey secretKey,
byte[] secretData, String algorithmParam)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, UnsupportedEncodingException,
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidAlgorithmParameterException {
Cipher decrypt = Cipher.getInstance(decryptType);
if (null == algorithmParam) {
decrypt.init(Cipher.DECRYPT_MODE, secretKey);
} else {
IvParameterSpec iv = new IvParameterSpec(
algorithmParam.getBytes(CHARSET));
decrypt.init(Cipher.DECRYPT_MODE, secretKey, iv);
}
byte[] srcData = decrypt.doFinal(secretData);
String srcStr = new String(srcData, CHARSET);
return srcStr;
}
}