Posted on 2008-05-23 23:21
acooly 阅读(522)
评论(0) 编辑 收藏
1 package org.acooly.studio.encrypt;
2
3 import java.security.Key;
4
5 import javax.crypto.Cipher;
6 import javax.crypto.KeyGenerator;
7 import javax.crypto.SecretKey;
8 import javax.crypto.spec.IvParameterSpec;
9 import javax.crypto.spec.SecretKeySpec;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 /**
15 * 对称加密JAVA算法演示(JAVA-SUN API实现)
16 *
17 *
18 * @author pu.zhang
19 *
20 */
21 public class SymmetricEncryption {
22
23 private Log logger = LogFactory.getLog(SymmetricEncryption.class);
24
25 public static void main(String[] args) throws Exception {
26
27 SymmetricEncryption symmetricEncryption = new SymmetricEncryption();
28
29 // DES
30 symmetricEncryption.DESEncryption();
31 // 3DES
32 symmetricEncryption.tripleDESEncryption();
33
34 }
35
36 /**
37 * DES加/解密演示
38 *
39 * @throws Exception
40 */
41 void DESEncryption() throws Exception {
42
43 // 明文
44 byte[] plainText = "I am plain text!".getBytes();
45
46 logger.info("明文:" + formatedHexString(plainText));
47 // 通过KeyGenerator形成一个key
48 KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
49 Key key = keyGenerator.generateKey();
50 logger.info("KEY:" + formatedHexString(key.getEncoded()));
51
52 // 获得一个私钥加密类Cipher,CBC是加密方式,PKCS5Padding是填充方法
53 // CBC加密方式:把明文分为左右两部分LP和RP
54 String transformation = "DES/CBC/PKCS5Padding"; // algorithm/mode/padding
55 Cipher cipher = Cipher.getInstance(transformation);
56
57 // CBC方式的初始化向量
58 byte[] iv = "iamaniv.".getBytes();
59 IvParameterSpec ivparam = new IvParameterSpec(iv);
60 // 加密
61 cipher.init(Cipher.ENCRYPT_MODE, key, ivparam);
62 byte[] cipherText = cipher.doFinal(plainText);
63 logger.info("加密后密文:" + formatedHexString(cipherText));
64
65 // 解密
66
67 cipher.init(Cipher.DECRYPT_MODE, key, ivparam);
68 byte[] newPlainText = cipher.doFinal(cipherText);
69 logger.info("解密后明文:" + formatedHexString(newPlainText));
70 }
71
72 /**
73 * DES加/解密演示
74 *
75 * @throws Exception
76 */
77 void tripleDESEncryption() throws Exception {
78 // 24字节密钥key,3倍DES密钥长度
79 byte[] tripleKey = "123456789012345678901234".getBytes();
80 logger.info("tripleKey:" + formatedHexString(tripleKey));
81 // 明文
82 byte[] plainText = "I am plain text!".getBytes();
83 logger.info("初始的明文:" + formatedHexString(plainText));
84
85 // 算法
86 String algorithm = "DESede";
87
88 // 生成密钥
89 SecretKey secretKey = new SecretKeySpec(tripleKey, algorithm);
90
91 String transformation = "DESede/CBC/PKCS5Padding";
92 Cipher cipher = Cipher.getInstance(transformation);
93 // CBC方式的初始化向量
94 byte[] iv = "iamaniv.".getBytes();
95 IvParameterSpec ivparam = new IvParameterSpec(iv);
96
97 // 加密
98 cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivparam);
99 byte[] encriptText = cipher.doFinal(plainText);
100 logger.info("加密的密文:" + formatedHexString(encriptText));
101
102 // 解密码
103 cipher.init(Cipher.DECRYPT_MODE, secretKey, ivparam);
104 byte[] newPlainText = cipher.doFinal(encriptText);
105 logger.info("解密的明文:" + formatedHexString(newPlainText));
106 }
107
108
109 /**
110 * 转换byte数组为16进制的字符串显示方式
111 * @param b
112 * @return
113 */
114 String formatedHexString(byte[] b) {
115 String hs = "";
116 String stmp = "";
117
118 for (int n = 0; n < b.length; n++) {
119 stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
120 if (stmp.length() == 1)
121 hs = hs + "0" + stmp;
122 else
123 hs = hs + stmp;
124 if (n < b.length - 1)
125 hs = hs + " ";
126 }
127 return hs.toUpperCase();
128 }
129
130 }
131