1 package com.IBus.Servlet;
2
3 import java.security.spec.KeySpec;
4
5 import javax.crypto.Cipher;
6 import javax.crypto.SecretKey;
7 import javax.crypto.SecretKeyFactory;
8 import javax.crypto.spec.DESKeySpec;
9
10 /**
11 * DES加密、解密
12 * @ClassName DES
13 * @author 张月
14 * @date 2013年8月8日
15 */
16 public class DES{
17
18 /**
19 * DES加密
20 * @param HexString 字符串(16位16进制字符串)
21 * @param keyStr 密钥16个1
22 * @throws Exception
23 */
24 public static byte[] SEncrypt_DES(byte[] HexString,byte[] keyStr) throws Exception{
25 try {
26 byte[] theCph = new byte[8];
27 try {
28 byte[] theKey = null;
29 byte[] theMsg = null;
30 theMsg = HexString;
31 theKey = keyStr;
32 KeySpec ks = new DESKeySpec(theKey);
33 SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
34 SecretKey ky = kf.generateSecret(ks);
35 Cipher cf = Cipher.getInstance("DES/ECB/NoPadding");
36 cf.init(Cipher.ENCRYPT_MODE,ky);
37 theCph = cf.doFinal(theMsg);
38 // System.out.println("*************DES加密****************");
39 // System.out.println("密钥 : "+bytesToHex(keyStr));
40 // System.out.println("字符串: "+bytesToHex(theMsg));
41 // System.out.println("加密后: "+bytesToHex(theCph));
42 } catch (Exception e) {
43 e.printStackTrace();
44 }
45 return theCph;
46 } catch (Exception e) {
47 throw e;
48 }
49 }
50
51 // //进行ECB模式的DES加密,已验证成功
52 // public static void main(String[] args) throws Exception {
53 // byte[] s = SEncrypt_DES(PBOCDESConvertUtil.hexStringToByte("0123456789ABCDEF"),PBOCDESConvertUtil.hexStringToByte("1111111111111111"));
54 // System.out.println(s.length);
55 // }
56
57 /**
58 * DES解密
59 *
60 * @param hexStr 16位十六进制字符串
61 * @param keyStr 密钥16个1
62 * @param modeStr 解密模式:ECB
63 * @throws Exception
64 */
65 public static byte[] SDecrypt_DES(byte[] hexStr,byte[] keyStr) throws Exception{
66 try {
67 String algorithm = "DES/ECB/NoPadding";
68 byte[] theCph = new byte[8];
69 byte[] theKey = null;
70 byte[] theMsg = null;
71 theMsg = hexStr;
72 theKey = keyStr;
73 KeySpec ks = new DESKeySpec(theKey);
74 SecretKeyFactory kf
75 = SecretKeyFactory.getInstance("DES");
76 SecretKey ky = kf.generateSecret(ks);
77 Cipher cf = Cipher.getInstance(algorithm);
78 cf.init(Cipher.DECRYPT_MODE,ky);
79 theCph = cf.doFinal(theMsg);
80 // System.out.println("*************DES解密****************");
81 // System.out.println("密钥 : "+bytesToHex(theKey));
82 // System.out.println("字符串: "+bytesToHex(theMsg));
83 // System.out.println("解密后: "+bytesToHex(theCph));
84
85 return theCph;
86
87 } catch (Exception e) {
88 throw e;
89 }
90 }
91
92 public static byte[] hexToBytes(String str) {
93 try {
94 if (str==null) {
95 return null;
96 } else if (str.length() < 2) {
97 return null;
98 } else {
99 int len = str.length() / 2;
100 byte[] buffer = new byte[len];
101 for (int i=0; i<len; i++) {
102 buffer[i] = (byte) Integer.parseInt(
103 str.substring(i*2,i*2+2),16);
104 }
105 return buffer;
106 }
107
108 } catch (Exception e) {
109 throw e;
110 }
111 }
112 public static String bytesToHex(byte[] data) {
113 try {
114 if (data==null) {
115 return null;
116 } else {
117 int len = data.length;
118 String str = "";
119 for (int i=0; i<len; i++) {
120 if ((data[i]&0xFF)<16) str = str + "0"
121 + java.lang.Integer.toHexString(data[i]&0xFF);
122 else str = str
123 + java.lang.Integer.toHexString(data[i]&0xFF);
124 }
125 return str.toUpperCase();
126 }
127 } catch (Exception e) {
128 throw e;
129 }
130 }
131
132
133 }
1 package com.IBus.Servlet;
2
3 //PBOCDES算法
4 public class PBOCDES {
5
6 public static void main(String[] args) {
7 //测试
8 // String str= "182012121102335900010000000100000";
9 String str= "000001000200000031323320150715181901";
10 // String str= "303030303031303030323030303030303331333233333230313530373135313831393031";
11 String result = Get_PBOC_DES_HEX(str,"D4A2A9F58CA33E75", "0000000000000000");
12 System.out.println(result);
13 }
14
15 /*
16 * PBOCDES加密
17 * @param shuju:加密的数据的byte[]
18 * @param key:密钥 16位十六进制
19 * @param IV:初始向量,默认为0000000000000000
20 */
21 public static String Get_PBOC_DES_ASCII(byte[] shuju, String key, String IV)
22 {
23 String returntype = "";
24 try
25 {
26 //----------------------------------------
27 byte[] keyss = new byte[8];
28 byte[] IVS = new byte[8];
29 keyss = PBOCDESConvertUtil.hexStringToByte(key);
30 IVS = PBOCDESConvertUtil.hexStringToByte(IV);
31 //----------------------------------------
32 byte[] keys = keyss;
33 //数据内容字节数组
34 String slshuju = PBOCDESConvertUtil.bytesToHexString(shuju);
35 int TLen = 0;
36 int DBz = 0;
37 if (slshuju.length() % 16 != 0 || slshuju.length() % 16 == 0)
38 {
39 TLen = (((int)(slshuju.length() / 16)) + 1) * 16;
40 DBz = (slshuju.length() / 16) + 1;
41 slshuju = slshuju + "8";
42 TLen = TLen - slshuju.length();
43 for (int i = 0; i < TLen; i++)
44 {
45 slshuju = slshuju + "0";
46 }
47 }
48 byte[] Zshuju = new byte[slshuju.length() / 2];
49 Zshuju = PBOCDESConvertUtil.hexStringToByte(slshuju);
50
51 byte[] D1 = new byte[8];
52 byte[] D2 = new byte[8];
53 byte[] I2 = new byte[8];
54 byte[] I3 = new byte[8];
55 byte[] bytTemp = new byte[8];
56 byte[] bytTempX = new byte[8];
57 //初始向量
58 byte[] I0 = IVS;
59 if (DBz >= 1)
60 {
61 for (int i = 0; i < 8; i++)
62 {
63 D1[i] = Zshuju[i];
64 }
65 for (int i = 0; i < 8; i++)
66 {
67 bytTemp[i] = (byte)(I0[i] ^ D1[i]);
68 }
69 I2 = bytTemp;
70 bytTempX = DES.SEncrypt_DES(I2, keys);
71 }
72 if (DBz >= 2)
73 {
74 for (int j = 2; j <= DBz; j++)
75 {
76 for (int i = (j - 1) * 8; i < j * 8; i++)
77 {
78 D2[i - (j - 1) * 8] = Zshuju[i];
79 }
80 for (int i = 0; i < 8; i++)
81 {
82 bytTemp[i] = (byte)(bytTempX[i] ^ D2[i]);
83 }
84 I3 = bytTemp;
85 bytTempX = DES.SEncrypt_DES(I3, keys);
86 }
87 }
88
89 returntype = PBOCDESConvertUtil.bytesToHexString(bytTempX);
90
91 }catch(Exception e)
92 {
93 returntype = "";
94 }
95 return returntype;
96 }
97
98 /*
99 * PBOCDES加密
100 * @param shuju:加密的数据的byte[]
101 * @param key:密钥 16位十六进制
102 * @param IV:初始向量,默认为0000000000000000
103 */
104 public static String Get_PBOC_DES_HEX(String slshuju, String key, String IV)
105 {
106 String returntype = "";
107 try
108 {
109 //----------------------------------------
110 byte[] keyss = new byte[8];
111 byte[] IVS = new byte[8];
112 keyss = PBOCDESConvertUtil.hexStringToByte(key);
113 IVS = PBOCDESConvertUtil.hexStringToByte(IV);
114 //----------------------------------------
115 byte[] keys = keyss;
116 int TLen = 0;
117 int DBz = 0;
118 if (slshuju.length() % 16 != 0 || slshuju.length() % 16 == 0)
119 {
120 TLen = (((int)(slshuju.length() / 16)) + 1) * 16;
121 DBz = (slshuju.length() / 16) + 1;
122 slshuju = slshuju + "8";
123 TLen = TLen - slshuju.length();
124 for (int i = 0; i < TLen; i++)
125 {
126 slshuju = slshuju + "0";
127 }
128 }
129 byte[] Zshuju = new byte[slshuju.length() / 2];
130 Zshuju = PBOCDESConvertUtil.hexStringToByte(slshuju);
131
132 byte[] D1 = new byte[8];
133 byte[] D2 = new byte[8];
134 byte[] I2 = new byte[8];
135 byte[] I3 = new byte[8];
136 byte[] bytTemp = new byte[8];
137 byte[] bytTempX = new byte[8];
138 //初始向量
139 byte[] I0 = IVS;
140 if (DBz >= 1)
141 {
142 for (int i = 0; i < 8; i++)
143 {
144 D1[i] = Zshuju[i];
145 }
146 for (int i = 0; i < 8; i++)
147 {
148 bytTemp[i] = (byte)(I0[i] ^ D1[i]);
149 }
150 I2 = bytTemp;
151 bytTempX = DES.SEncrypt_DES(I2, keys);
152 }
153 if (DBz >= 2)
154 {
155 for (int j = 2; j <= DBz; j++)
156 {
157 for (int i = (j - 1) * 8; i < j * 8; i++)
158 {
159 D2[i - (j - 1) * 8] = Zshuju[i];
160 }
161 for (int i = 0; i < 8; i++)
162 {
163 bytTemp[i] = (byte)(bytTempX[i] ^ D2[i]);
164 }
165 I3 = bytTemp;
166 bytTempX = DES.SEncrypt_DES(I3, keys);
167 }
168 }
169
170 returntype = PBOCDESConvertUtil.bytesToHexString(bytTempX);
171
172 }catch(Exception e)
173 {
174 returntype = "";
175 }
176 return returntype;
177 }
178
179 }
1 package com.IBus.Servlet;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.ObjectInputStream;
7 import java.io.ObjectOutputStream;
8 import java.io.Serializable;
9 import java.security.MessageDigest;
10 import java.security.NoSuchAlgorithmException;
11
12 //帮助类
13 public class PBOCDESConvertUtil {
14 public final static char[] BToA = "0123456789abcdef".toCharArray();
15
16 private PBOCDESConvertUtil() {
17
18 }
19
20 /**
21 * 把16进制字符串转换成字节数组
22 *
23 * @param hex
24 * @return
25 */
26 public static byte[] hexStringToByte(String hex) {
27 int len = (hex.length() / 2);
28 byte[] result = new byte[len];
29 char[] achar = hex.toCharArray();
30 for (int i = 0; i < len; i++) {
31 int pos = i * 2;
32 result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
33 }
34 return result;
35 }
36
37 private static byte toByte(char c) {
38 byte b = (byte) "0123456789ABCDEF".indexOf(c);
39 return b;
40 }
41
42 /**
43 * 把字节数组转换成16进制字符串
44 *
45 * @param bArray
46 * @return
47 */
48 public static final String bytesToHexString(byte[] bArray) {
49 if(bArray == null )
50 {
51 return "";
52 }
53 StringBuffer sb = new StringBuffer(bArray.length);
54 String sTemp;
55 for (int i = 0; i < bArray.length; i++) {
56 sTemp = Integer.toHexString(0xFF & bArray[i]);
57 if (sTemp.length() < 2)
58 sb.append(0);
59 sb.append(sTemp.toUpperCase());
60 }
61 return sb.toString();
62 }
63
64 /**
65 * 把字节数组转换为对象
66 *
67 * @param bytes
68 * @return
69 * @throws IOException
70 * @throws ClassNotFoundException
71 */
72 public static final Object bytesToObject(byte[] bytes) throws IOException,
73 ClassNotFoundException {
74 ByteArrayInputStream in = new ByteArrayInputStream(bytes);
75 ObjectInputStream oi = new ObjectInputStream(in);
76 Object o = oi.readObject();
77 oi.close();
78 return o;
79 }
80
81 /**
82 * 把可序列化对象转换成字节数组
83 *
84 * @param s
85 * @return
86 * @throws IOException
87 */
88 public static final byte[] objectToBytes(Serializable s) throws IOException {
89 ByteArrayOutputStream out = new ByteArrayOutputStream();
90 ObjectOutputStream ot = new ObjectOutputStream(out);
91 ot.writeObject(s);
92 ot.flush();
93 ot.close();
94 return out.toByteArray();
95 }
96
97 public static final String objectToHexString(Serializable s)
98 throws IOException {
99 return bytesToHexString(objectToBytes(s));
100 }
101
102 public static final Object hexStringToObject(String hex)
103 throws IOException, ClassNotFoundException {
104 return bytesToObject(hexStringToByte(hex));
105 }
106
107 /**
108 * @函数功能: BCD码转为10进制串(阿拉伯数据)
109 * @输入参数: BCD码
110 * @输出结果: 10进制串
111 */
112 public static String bcd2Str(byte[] bytes) {
113 StringBuffer temp = new StringBuffer(bytes.length * 2);
114
115 for (int i = 0; i < bytes.length; i++) {
116 temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
117 temp.append((byte) (bytes[i] & 0x0f));
118 }
119 return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
120 .toString().substring(1) : temp.toString();
121 }
122
123 /**
124 * @函数功能: 10进制串转为BCD码
125 * @输入参数: 10进制串
126 * @输出结果: BCD码
127 */
128 public static byte[] str2Bcd(String asc) {
129 int len = asc.length();
130 int mod = len % 2;
131
132 if (mod != 0) {
133 asc = "0" + asc;
134 len = asc.length();
135 }
136
137 byte abt[] = new byte[len];
138 if (len >= 2) {
139 len = len / 2;
140 }
141
142 byte bbt[] = new byte[len];
143 abt = asc.getBytes();
144 int j, k;
145
146 for (int p = 0; p < asc.length() / 2; p++) {
147 if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
148 j = abt[2 * p] - '0';
149 } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
150 j = abt[2 * p] - 'a' + 0x0a;
151 } else {
152 j = abt[2 * p] - 'A' + 0x0a;
153 }
154
155 if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
156 k = abt[2 * p + 1] - '0';
157 } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
158 k = abt[2 * p + 1] - 'a' + 0x0a;
159 } else {
160 k = abt[2 * p + 1] - 'A' + 0x0a;
161 }
162
163 int a = (j << 4) + k;
164 byte b = (byte) a;
165 bbt[p] = b;
166 }
167 return bbt;
168 }
169
170 public static String BCD2ASC(byte[] bytes) {
171 StringBuffer temp = new StringBuffer(bytes.length * 2);
172
173 for (int i = 0; i < bytes.length; i++) {
174 int h = ((bytes[i] & 0xf0) >>> 4);
175 int l = (bytes[i] & 0x0f);
176 temp.append(BToA[h]).append(BToA[l]);
177 }
178 return temp.toString();
179 }
180
181 /**
182 * 两字符数组异或
183 */
184 public static byte[] byteArrXor(byte[] arr1, byte[] arr2, int len){
185 byte[] dest = new byte[len];
186
187 if((arr1.length < len) || (arr2.length < len)){
188 return null;
189 }
190
191 for(int i = 0;i < len;i++){
192 dest[i] = (byte)(arr1[i] ^ arr2[i]);
193 }
194
195 return dest;
196 }
197
198
199 /**
200 * MD5加密字符串,返回加密后的16进制字符串
201 * @param origin
202 * @return
203 */
204 public static String MD5EncodeToHex(String origin) {
205 return bytesToHexString(MD5Encode(origin));
206 }
207
208 /**
209 * MD5加密字符串,返回加密后的字节数组
210 *
211 * @param origin
212 * @return
213 */
214 public static byte[] MD5Encode(String origin) {
215 return MD5Encode(origin.getBytes());
216 }
217
218 /**
219 * MD5加密字节数组,返回加密后的字节数组
220 *
221 * @param bytes
222 * @return
223 */
224 public static byte[] MD5Encode(byte[] bytes) {
225 MessageDigest md = null;
226 try {
227 md = MessageDigest.getInstance("MD5");
228 return md.digest(bytes);
229 } catch (NoSuchAlgorithmException e) {
230 e.printStackTrace();
231 return new byte[0];
232 }
233
234 }
235 }