Skip to content

Commit eaefee5

Browse files
committed
重构:抽取可以在性能测试中公用的代码,方便复用
1 parent 84201db commit eaefee5

2 files changed

Lines changed: 35 additions & 18 deletions

File tree

src/cn/aofeng/demo/encrypt/AES.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
*/
2222
public class AES extends EncryptAndDecrypt {
2323

24+
private String encryptType = "AES/CBC/PKCS5Padding";
25+
private String algorithmParam = "abcdefgh12345678";
26+
27+
private void execute(String data) throws InvalidKeyException,
28+
IllegalBlockSizeException, BadPaddingException,
29+
UnsupportedEncodingException, NoSuchAlgorithmException,
30+
NoSuchPaddingException, InvalidAlgorithmParameterException {
31+
SecretKey secretKey = createSecretKey("AES", "abcdefgh_1234567");
32+
byte[] secretData = encrypt(encryptType, secretKey, data,
33+
algorithmParam);
34+
log("使用%s加密后的数据:", encryptType);
35+
log(Base64.encodeBase64String(secretData));
36+
37+
String srcStr = decrypt(encryptType, secretKey, secretData,
38+
algorithmParam);
39+
log("解密后的数据:\n%s", srcStr);
40+
}
41+
2442
public static void main(String[] args) throws UnsupportedEncodingException,
2543
InvalidKeyException, IllegalBlockSizeException,
2644
BadPaddingException, NoSuchAlgorithmException,
@@ -29,15 +47,7 @@ public static void main(String[] args) throws UnsupportedEncodingException,
2947
log("待加密的数据:\n%s", data);
3048

3149
AES aes = new AES();
32-
String encryptType = "AES/CBC/PKCS5Padding";
33-
String algorithmParam = "abcdefgh12345678";
34-
SecretKey secretKey = aes.createSecretKey("AES", "abcdefgh_1234567");
35-
byte[] secretData = aes.encrypt(encryptType, secretKey, data, algorithmParam);
36-
log("使用%s加密后的数据:", encryptType);
37-
log(Base64.encodeBase64String(secretData));
38-
39-
String srcStr = aes.decrypt(encryptType, secretKey, secretData, algorithmParam);
40-
log("解密后的数据:\n%s", srcStr);
50+
aes.execute(data);
4151
}
4252

4353
}

src/cn/aofeng/demo/encrypt/Blowfish.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@
2121
*/
2222
public class Blowfish extends EncryptAndDecrypt {
2323

24+
private String encryptType = "Blowfish";
25+
26+
private void execute(String data) throws InvalidKeyException,
27+
IllegalBlockSizeException, BadPaddingException,
28+
UnsupportedEncodingException, NoSuchAlgorithmException,
29+
NoSuchPaddingException, InvalidAlgorithmParameterException {
30+
SecretKey secretKey = createSecretKey(encryptType, "abcdefgh_1234567");
31+
byte[] secretData = encrypt(encryptType, secretKey, data);
32+
log("使用%s加密后的数据:", encryptType);
33+
log(Base64.encodeBase64String(secretData));
34+
35+
String srcStr = decrypt(encryptType, secretKey, secretData);
36+
log("解密后的数据:\n%s", srcStr);
37+
}
38+
2439
public static void main(String[] args) throws UnsupportedEncodingException,
2540
InvalidKeyException, IllegalBlockSizeException,
2641
BadPaddingException, NoSuchAlgorithmException,
@@ -29,15 +44,7 @@ public static void main(String[] args) throws UnsupportedEncodingException,
2944
log("待加密的数据:\n%s", data);
3045

3146
Blowfish bf = new Blowfish();
32-
String encryptType = "Blowfish";
33-
SecretKey secretKey = bf.createSecretKey(encryptType,
34-
"abcdefgh_1234567");
35-
byte[] secretData = bf.encrypt(encryptType, secretKey, data);
36-
log("使用%s加密后的数据:", encryptType);
37-
log(Base64.encodeBase64String(secretData));
38-
39-
String srcStr = bf.decrypt(encryptType, secretKey, secretData);
40-
log("解密后的数据:\n%s", srcStr);
47+
bf.execute(data);
4148
}
4249

4350
}

0 commit comments

Comments
 (0)