Skip to content

Commit f86f791

Browse files
committed
Blowfish加密与解密
1 parent 5296d56 commit f86f791

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

README.md

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

106106
##加密解密
107-
* [Blowfish与AES性能对比](src/cn/aofeng/demo/encrypt/EncryptAndDecrypt.java)
107+
* [加密解密基础类](src/cn/aofeng/demo/encrypt/EncryptAndDecrypt.java)
108+
* [Blowfish加密与解密](src/cn/aofeng/demo/encrypt/Blowfish.java)
109+
108110

109111
#Java企业开发
110112
##开源框架
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
import org.apache.commons.codec.binary.Base64;
16+
17+
/**
18+
* Blowfish加密解密。
19+
*
20+
* @author <a href="mailto:[email protected]">聂勇</a>
21+
*/
22+
public class Blowfish extends EncryptAndDecrypt {
23+
24+
public static void main(String[] args) throws UnsupportedEncodingException,
25+
InvalidKeyException, IllegalBlockSizeException,
26+
BadPaddingException, NoSuchAlgorithmException,
27+
NoSuchPaddingException, InvalidAlgorithmParameterException {
28+
String data = "炎黄,汉字,english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-";
29+
log("待加密的数据:\n%s", data);
30+
31+
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);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)