1 package com.time1.dao;
2 import java.security.Key;
3 import java.security.Security;
4
5 import javax.crypto.Cipher;
6
7 /**
8 * @Title: DES可逆加密算法:可自定义密钥
9 * @Description:
10 * @Author:zhoupk
11 * @Create:Jan 27, 2011 3:02:18 PM
12 * @Version:1.1
13 */
14 public class EncryDES {
15 private static String strDefaultKey = "szlcsoft";// 默认秘钥
16 private Cipher encryptCipher = null;
17 private Cipher decryptCipher = null;
18
19 /**
20 * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
21 * hexStr2ByteArr(String strIn) 互为可逆的转换过程
22 *
23 * @param arrB
24 * 需要转换的byte数组
25 * @return 转换后的字符串
26 * @throws Exception
27 * 本方法不处理任何异常,所有异常全部抛出
28 */
29 public static String byteArr2HexStr(byte[] arrB) throws Exception {
30 int iLen = arrB.length;// arrB 需要转换的byte数组
31 // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
32 // 每个字节用两个字符表示
33 StringBuffer sb = new StringBuffer(iLen * 2);
34 for (int i = 0; i < iLen; i++) {
35 int intTmp = arrB[i];
36 // 把负数转换为正数
37 while (intTmp < 0) {
38 intTmp = intTmp + 256;
39 }
40 // 小于0F的数需要在前面补0
41 if (intTmp < 16) {
42 sb.append("0");
43 }
44 sb.append(Integer.toString(intTmp, 16));
45 }
46 return sb.toString();
47 }
48
49 /**
50 * 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
51 * 互为可逆的转换过程
52 *
53 * @param strIn
54 * 需要转换的字符串
55 * @return 转换后的byte数组
56 * @throws Exception
57 * 本方法不处理任何异常,所有异常全部抛出
58 * @author <a href="mailto:wangchongan@gmail.com">WangChongAn</a>
59 */
60 public static byte[] hexStr2ByteArr(String strIn) throws Exception {
61 byte[] arrB = strIn.getBytes();
62 int iLen = arrB.length;
63
64 // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
65 byte[] arrOut = new byte[iLen / 2];
66 for (int i = 0; i < iLen; i = i + 2) {
67 String strTmp = new String(arrB, i, 2);
68 arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
69 }
70 return arrOut;
71 }
72
73 /**
74 * 默认构造方法,使用默认密钥
75 *
76 * @throws Exception
77 */
78 public EncryDES() throws Exception {
79 this(strDefaultKey);
80 }
81
82 /**
83 * 指定密钥构造方法
84 *
85 * @param strKey
86 * 指定的密钥
87 * @throws Exception
88 */
89 public EncryDES(String strKey) throws Exception {
90 Security.addProvider(new com.sun.crypto.provider.SunJCE());
91 Key key = getKey(strKey.getBytes());
92
93 encryptCipher = Cipher.getInstance("DES");
94 encryptCipher.init(Cipher.ENCRYPT_MODE, key);
95
96 decryptCipher = Cipher.getInstance("DES");
97 decryptCipher.init(Cipher.DECRYPT_MODE, key);
98 }
99
100 /**
101 * 加密字节数组
102 *
103 * @param arrB
104 * 需加密的字节数组
105 * @return 加密后的字节数组
106 * @throws Exception
107 */
108 public byte[] encrypt(byte[] arrB) throws Exception {
109 return encryptCipher.doFinal(arrB);
110 }
111
112 /**
113 * 加密字符串
114 *
115 * @param strIn
116 * 需加密的字符串
117 * @return 加密后的字符串
118 * @throws Exception
119 */
120 public String encrypt(String strIn) throws Exception {
121 return byteArr2HexStr(encrypt(strIn.getBytes()));
122 }
123
124 /**
125 * 解密字节数组
126 *
127 * @param arrB
128 * 需解密的字节数组
129 * @return 解密后的字节数组
130 * @throws Exception
131 */
132 public byte[] decrypt(byte[] arrB) throws Exception {
133 return decryptCipher.doFinal(arrB);
134 }
135
136 /**
137 * 解密字符串
138 *
139 * @param strIn
140 * 需解密的字符串
141 * @return 解密后的字符串
142 * @throws Exception
143 */
144 public String decrypt(String strIn) throws Exception {
145 return new String(decrypt(hexStr2ByteArr(strIn)));
146 }
147
148 /**
149 * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
150 *
151 * @param arrBTmp
152 * 构成该字符串的字节数组
153 * @return 生成的密钥
154 * @throws java.lang.Exception
155 */
156 private Key getKey(byte[] arrBTmp) throws Exception {
157 // 创建一个空的8位字节数组(默认值为0)
158 byte[] arrB = new byte[8]; // 将原始字节数组转换为8位
159 for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
160 arrB[i] = arrBTmp[i];
161 }
162 // 生成密钥
163 Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
164 return key;
165 }
166
167 public static void main(String[] args) throws Exception {
168
169 EncryDES test=new EncryDES();
170 System.out.println(test.encrypt("helen"));
171 System.out.println(test.decrypt(test.encrypt("helen")));
172
173
174 }
175
176 }