Skip to content

Commit 89cf1dd

Browse files
committed
Blowfish, AES性能比较
1 parent eaefee5 commit 89cf1dd

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@
104104
* [在JVM中编译脚本提升运行性能](http://aofengblog.blog.163.com/blog/static/6317021201311143045607/)
105105

106106
##加密解密
107-
* [加密解密基础类](src/cn/aofeng/demo/encrypt/EncryptAndDecrypt.java)
108107
* [Blowfish加密与解密](src/cn/aofeng/demo/encrypt/Blowfish.java)
109108
* [AES加密与解密](src/cn/aofeng/demo/encrypt/AES.java)
109+
* [Blowfish, AES性能比较](src/cn/aofeng/demo/encrypt/PerformanceCompare.java)
110110

111111
#Java企业开发
112112
##开源框架
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cn.aofeng.demo.encrypt;
2+
3+
import static cn.aofeng.demo.util.LogUtil.log;
4+
5+
import java.io.UnsupportedEncodingException;
6+
import java.security.InvalidAlgorithmParameterException;
7+
import java.security.InvalidKeyException;
8+
import java.security.NoSuchAlgorithmException;
9+
10+
import javax.crypto.BadPaddingException;
11+
import javax.crypto.IllegalBlockSizeException;
12+
import javax.crypto.NoSuchPaddingException;
13+
import javax.crypto.SecretKey;
14+
15+
/**
16+
* 加密解密性能比较。
17+
*
18+
* @author <a href="mailto:[email protected]">聂勇</a>
19+
*/
20+
public class PerformanceCompare extends EncryptAndDecrypt {
21+
22+
public void blowfishPerformence(String data)
23+
throws UnsupportedEncodingException, InvalidKeyException,
24+
IllegalBlockSizeException, BadPaddingException,
25+
NoSuchAlgorithmException, NoSuchPaddingException,
26+
InvalidAlgorithmParameterException {
27+
Blowfish bf = new Blowfish();
28+
SecretKey secretKey = createSecretKey(bf.encryptType, bf.key);
29+
long startTime = System.currentTimeMillis();
30+
for (int j = 0; j < 100000; j++) {
31+
bf.encrypt(bf.encryptType, secretKey, data+j);
32+
}
33+
long endTime = System.currentTimeMillis();
34+
long usedTime = endTime - startTime;
35+
log("使用%s进行%d次加密消耗时间%d毫秒", bf.encryptType, 100000, usedTime);
36+
}
37+
38+
public void aesPerformence(String data)
39+
throws UnsupportedEncodingException, InvalidKeyException,
40+
IllegalBlockSizeException, BadPaddingException,
41+
NoSuchAlgorithmException, NoSuchPaddingException,
42+
InvalidAlgorithmParameterException {
43+
AES aes = new AES();
44+
SecretKey secretKey = createSecretKey("AES", aes.key);
45+
long startTime = System.currentTimeMillis();
46+
for (int j = 0; j < 100000; j++) {
47+
aes.encrypt(aes.encryptType, secretKey, data+j,
48+
aes.algorithmParam);
49+
}
50+
long endTime = System.currentTimeMillis();
51+
long usedTime = endTime - startTime;
52+
log("使用%s进行%d次加密消耗时间%d毫秒", aes.encryptType, 100000, usedTime);
53+
}
54+
55+
public static void main(String[] args) throws InvalidKeyException,
56+
UnsupportedEncodingException, IllegalBlockSizeException,
57+
BadPaddingException, NoSuchAlgorithmException,
58+
NoSuchPaddingException, InvalidAlgorithmParameterException {
59+
String data = "炎黄,汉字,english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-";
60+
log("待加密的数据:\n%s", data);
61+
62+
PerformanceCompare pc = new PerformanceCompare();
63+
// AES
64+
pc.aesPerformence(data);
65+
66+
// Blowfish
67+
pc.blowfishPerformence(data);
68+
}
69+
70+
}

0 commit comments

Comments
 (0)