Skip to content

Commit 5296d56

Browse files
committed
1、重构:只提供加密和解密操作,具体的灱操作由具体的子类实现。
2、完善注释。
1 parent 787d98b commit 5296d56

1 file changed

Lines changed: 61 additions & 92 deletions

File tree

‎src/cn/aofeng/demo/encrypt/EncryptAndDecrypt.java‎

Lines changed: 61 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
import javax.crypto.spec.IvParameterSpec;
1414
import javax.crypto.spec.SecretKeySpec;
1515

16-
import org.apache.commons.codec.binary.Base64;
17-
18-
import static cn.aofeng.demo.util.LogUtil.log;;
19-
2016
/**
2117
* 加密与解密。
2218
*
@@ -25,123 +21,96 @@
2521
public class EncryptAndDecrypt {
2622

2723
private final static String CHARSET = "utf8";
28-
29-
private SecretKey createSecretKey(String encryptType, String keyStr)
24+
25+
/**
26+
* 创建安全密钥。
27+
*
28+
* @param encryptType
29+
* 加密方式,如: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>
30+
* @param keyStr
31+
* 密钥明文
32+
* @return 安全密钥
33+
* @throws UnsupportedEncodingException
34+
* 不支持指定的字符集编码
35+
*/
36+
public SecretKey createSecretKey(String encryptType, String keyStr)
3037
throws UnsupportedEncodingException {
3138
byte[] secretKeyData = keyStr.getBytes(CHARSET);
3239
SecretKeySpec sks = new SecretKeySpec(secretKeyData, encryptType);
33-
40+
3441
return sks;
3542
}
36-
37-
private byte[] encrypt(String encryptType, SecretKey secretKey, String srcData)
38-
throws InvalidKeyException, IllegalBlockSizeException,
39-
BadPaddingException, UnsupportedEncodingException,
40-
NoSuchAlgorithmException, NoSuchPaddingException,
41-
InvalidAlgorithmParameterException {
43+
44+
/**
45+
* 加密数据。
46+
*
47+
* @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>
48+
* @param secretKey 密钥
49+
* @param srcData 待加密的数据
50+
* @return 数据加密后的字节数组
51+
* @see #encrypt(String, SecretKey, String, String)
52+
*/
53+
public byte[] encrypt(String encryptType, SecretKey secretKey,
54+
String srcData) throws InvalidKeyException,
55+
IllegalBlockSizeException, BadPaddingException,
56+
UnsupportedEncodingException, NoSuchAlgorithmException,
57+
NoSuchPaddingException, InvalidAlgorithmParameterException {
4258
return encrypt(encryptType, secretKey, srcData, null);
4359
}
44-
45-
private byte[] encrypt(String encryptType, SecretKey secretKey, String srcData, String algorithmParam)
46-
throws InvalidKeyException, IllegalBlockSizeException,
47-
BadPaddingException, UnsupportedEncodingException,
48-
NoSuchAlgorithmException, NoSuchPaddingException,
49-
InvalidAlgorithmParameterException {
60+
61+
public byte[] encrypt(String encryptType, SecretKey secretKey,
62+
String srcData, String algorithmParam) throws InvalidKeyException,
63+
IllegalBlockSizeException, BadPaddingException,
64+
UnsupportedEncodingException, NoSuchAlgorithmException,
65+
NoSuchPaddingException, InvalidAlgorithmParameterException {
5066
Cipher encrpyt = Cipher.getInstance(encryptType);
5167
if (null == algorithmParam) {
5268
encrpyt.init(Cipher.ENCRYPT_MODE, secretKey);
5369
} else {
54-
IvParameterSpec iv = new IvParameterSpec(algorithmParam.getBytes(CHARSET));
70+
IvParameterSpec iv = new IvParameterSpec(
71+
algorithmParam.getBytes(CHARSET));
5572
encrpyt.init(Cipher.ENCRYPT_MODE, secretKey, iv);
5673
}
5774
byte[] secretData = encrpyt.doFinal(srcData.getBytes(CHARSET));
58-
75+
5976
return secretData;
6077
}
61-
62-
private String decrypt(String decryptType, SecretKey secretKey, byte[] secretData)
63-
throws InvalidKeyException, IllegalBlockSizeException,
64-
BadPaddingException, UnsupportedEncodingException,
65-
NoSuchAlgorithmException, NoSuchPaddingException,
66-
InvalidAlgorithmParameterException {
78+
79+
/**
80+
* 解密数据。
81+
*
82+
* @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>
83+
* @param secretKey 密钥
84+
* @param secretData 待解密的数据
85+
* @return 解密后的数据
86+
* @see #decrypt(String, SecretKey, byte[], String)
87+
*/
88+
public String decrypt(String decryptType, SecretKey secretKey,
89+
byte[] secretData) throws InvalidKeyException,
90+
IllegalBlockSizeException, BadPaddingException,
91+
UnsupportedEncodingException, NoSuchAlgorithmException,
92+
NoSuchPaddingException, InvalidAlgorithmParameterException {
6793
return decrypt(decryptType, secretKey, secretData, null);
6894
}
69-
70-
private String decrypt(String decryptType, SecretKey secretKey, byte[] secretData, String algorithmParam)
95+
96+
public String decrypt(String decryptType, SecretKey secretKey,
97+
byte[] secretData, String algorithmParam)
7198
throws InvalidKeyException, IllegalBlockSizeException,
7299
BadPaddingException, UnsupportedEncodingException,
73-
NoSuchAlgorithmException, NoSuchPaddingException,
100+
NoSuchAlgorithmException, NoSuchPaddingException,
74101
InvalidAlgorithmParameterException {
75102
Cipher decrypt = Cipher.getInstance(decryptType);
76103
if (null == algorithmParam) {
77104
decrypt.init(Cipher.DECRYPT_MODE, secretKey);
78105
} else {
79-
IvParameterSpec iv = new IvParameterSpec(algorithmParam.getBytes(CHARSET));
106+
IvParameterSpec iv = new IvParameterSpec(
107+
algorithmParam.getBytes(CHARSET));
80108
decrypt.init(Cipher.DECRYPT_MODE, secretKey, iv);
81109
}
82-
byte[] srcData = decrypt.doFinal(secretData);
110+
byte[] srcData = decrypt.doFinal(secretData);
83111
String srcStr = new String(srcData, CHARSET);
84-
112+
85113
return srcStr;
86114
}
87-
88-
private void blowfishPerformence(String data)
89-
throws UnsupportedEncodingException, InvalidKeyException,
90-
IllegalBlockSizeException, BadPaddingException,
91-
NoSuchAlgorithmException, NoSuchPaddingException,
92-
InvalidAlgorithmParameterException {
93-
SecretKey secretKey;
94-
String type = "Blowfish";
95-
secretKey = createSecretKey(type, "abcdefgh_1234567");
96-
long startTime = System.currentTimeMillis();
97-
for (int j = 0; j < 100000; j++) {
98-
encrypt(type, secretKey, data+j);
99-
}
100-
long endTime = System.currentTimeMillis();
101-
long usedTime = endTime - startTime;
102-
log("使用%s进行%d次加密消耗时间%d毫秒", type, 100000, usedTime);
103-
}
104-
105-
private void aesPerformence(String data)
106-
throws UnsupportedEncodingException, InvalidKeyException,
107-
IllegalBlockSizeException, BadPaddingException,
108-
NoSuchAlgorithmException, NoSuchPaddingException,
109-
InvalidAlgorithmParameterException {
110-
SecretKey secretKey;
111-
String type = "AES/CBC/PKCS5Padding";
112-
secretKey = createSecretKey("AES", "abcdefgh_1234567");
113-
long startTime = System.currentTimeMillis();
114-
for (int j = 0; j < 100000; j++) {
115-
encrypt(type, secretKey, data+j, "abcdefgh12345678");
116-
}
117-
long endTime = System.currentTimeMillis();
118-
long usedTime = endTime - startTime;
119-
log("使用%s进行%d次加密消耗时间%d毫秒", type, 100000, usedTime);
120-
}
121-
122-
public static void main(String[] args) throws NoSuchAlgorithmException,
123-
NoSuchPaddingException, UnsupportedEncodingException,
124-
InvalidKeyException, IllegalBlockSizeException,
125-
BadPaddingException, InvalidAlgorithmParameterException {
126-
String data = "炎黄,汉字,english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-";
127-
log("待加密的数据:\n%s", data);
128-
129-
String encryptType = "Blowfish";
130-
EncryptAndDecrypt bf = new EncryptAndDecrypt();
131-
SecretKey secretKey = bf.createSecretKey(encryptType, "abcdefgh_1234567");
132-
byte[] secretData = bf.encrypt(encryptType, secretKey, data);
133-
log("使用%s加密后的数据:", encryptType);
134-
log(Base64.encodeBase64String(secretData));
135-
136-
String srcStr = bf.decrypt(encryptType, secretKey, secretData);
137-
log("解密后的数据:\n%s", srcStr);
138-
139-
// ==========性能测试==========
140-
// AES
141-
bf.aesPerformence(data);
142-
143-
// Blowfish
144-
bf.blowfishPerformence(data);
145-
}
146115

147116
}

0 commit comments

Comments
 (0)