-
Notifications
You must be signed in to change notification settings - Fork 3
/
Encrypt.java
351 lines (305 loc) · 7.63 KB
/
Encrypt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package com.zj.qcup.util.iso8583;
import com.zj.core.web.interceptor.SysOutInterceptor;
/**
* @author wangl
*
*/
public class Encrypt {
public static final int SUFFIX = 1; // 后面补零
public static final int PREFIX = 2; // 前面补零
/**
* @brief
* 用主密钥加密mak,pik,带checkvalue
*
* @param mak
* @param kek
* @return
*/
public static byte[] encryptKey(byte[] key,byte[] kek) {
byte[] out = new byte[12];
System.arraycopy(encrypt(key,kek),0,out,0,8);
System.arraycopy(encrypt(new byte[8], key),0,out,8,4);
return out;
}
/**
* des加密
*
*
* @param input
* @param key
* @return
*/
public static byte[] encrypt(byte[] input, byte[] key) {
long[] keynum = new long[16];
Des.des_set_key(key, keynum);
byte[] encypted = new byte[8];
Des.ecb_encrypt(input, encypted, keynum, true);
return encypted;
}
/**
* des解密
*
* @param input
* @param key
* @return
*/
public static byte[] decrypt(byte[] input, byte[] key) {
long[] keynum = new long[16];
Des.des_set_key(key, keynum);
byte[] encypted = new byte[8];
Des.ecb_encrypt(input, encypted, keynum, false);
return encypted;
}
/**
* 3DES加密
* @param input 要加密数据
* @param key 密钥
* @return
*/
public static byte[] TriDESencrypt(byte[] input,byte[] key){
byte[] key1 = new byte[8];
byte[] key2 = new byte[8];
System.arraycopy(key, 0, key1, 0, key1.length);
System.arraycopy(key, 8, key2, 0, key2.length);
byte[] ret = encrypt(input, key1);
ret = decrypt(ret, key2);
return encrypt(ret, key1);
}
/**
* 3DES加密
* @param input 要解密数据
* @param key 密钥
* @return
*/
public static byte[] TriDESdecrypt(byte[] input,byte[] key){
byte[] key1 = new byte[8];
byte[] key2 = new byte[8];
System.arraycopy(key, 0, key1, 0, key1.length);
System.arraycopy(key, 8, key2, 0, key2.length);
byte[] ret = decrypt(input, key1);
ret = encrypt(ret, key2);
return decrypt(ret, key1);
}
/**
* ECB加密
*
* @param input
* @param MAK
* @return
*/
public static byte[] ECB(byte[] input, byte[] MAK) {
// -- P1&&P2
int len = input.length / 8;
if (len < 8 || input.length % 8 != 0)
len++;
byte[] tem = new byte[len * 8];
System.arraycopy(input, 0, tem, 0, input.length);
input = tem;
byte[] M1 = new byte[8];
System.arraycopy(input, 0, M1, 0, 8);
byte[] MB = new byte[8];
for (int i = 1; i < len; i++) {
System.arraycopy(input, 8 * i, MB, 0, 8);
M1 = XOR(M1, MB);
}
byte[] result = M1;
// --p3 2hex
String block = byte2hex(result);
// --p4 MAK encrypt
byte[] b1 = encrypt(block.substring(0, 8).getBytes(), MAK);
// -p5 XOR
byte[] tempBlock = XOR(b1, block.substring(8).getBytes());
// --p6 des
byte[] b2 = encrypt(tempBlock, MAK);
// --p7 2hex
System.out.println("Mac密钥"+byte2hex(b2));
byte[] out = byte2hex(b2).substring(0, 8).getBytes();
return out;
}
/**
* 格式化byte
*
* @param b
* @return
*/
private static String byte2hex(byte[] b) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] out = new char[b.length * 2];
for (int i = 0; i < b.length; i++) {
byte c = b[i];
out[i * 2] = Digit[(c >>> 4) & 0X0F];
out[i * 2 + 1] = Digit[c & 0X0F];
}
return new String(out);
}
/**
* 异或
*
* @param op1
* @param op2
* @return
*/
private static byte[] XOR(byte[] op1, byte[] op2) {
int len = op1.length;
byte[] out = new byte[len];
for (int i = 0; i < len; i++) {
out[i] = (byte) (op1[i] ^ op2[i]);
}
return out;
}
public static void main(String[] args) {
System.out.println(ArrayUtil.hexEncode(XOR(ArrayUtil.hexDecode("5E4C9485F7CEC4159B469B9B9B58FD4C"), ArrayUtil.hexDecode("B8630C695022C3E2950FAF1171810696"))));
byte [] miyue = XOR(ArrayUtil.hexDecode("5E4C9485F7CEC4159B469B9B9B58FD4C"), ArrayUtil.hexDecode("B8630C695022C3E2950FAF1171810696"));
System.out.println(ArrayUtil.hexEncode(TriDESencrypt(ArrayUtil.hexDecode("00000000"), miyue)));
}
/**
* PIN加密
* @param pin 密码
* @param pan 卡号
* @param pik Pik
* @return
*/
public static byte[] encryptPIN(String pin,String pan,byte[] pik) {
//输入判断的防御式代码
if (pin == null || pan == null || pik == null) {
return null;
}
if (pan.length()<13) {
return null;
}
pan = pan.substring(pan.length()-13,pan.length()-1);
//获得PAN
byte[] subedpan = new byte[8];
byte[] input = ArrayUtil.hexDecode(pan);
System.arraycopy(input, 0, subedpan, 2, 6);
//获得PINBLOCK
byte[] pinblock = new byte[8];
// 填充FF
for (int i = 0; i < pinblock.length; i++) {
pinblock[i] = (byte) 0xFF;
}
int size = pin.length();
String t;
if (size <10) {
t = "0"+size+pin;
} else {
t = size+pin;
}
byte [] p = ArrayUtil.hexDecode(t);
System.arraycopy(p, 0, pinblock, 0, p.length);
if (pik.length == 8) {
return encrypt(XOR(pinblock, subedpan), pik);
} else {
return TriDESencrypt(XOR(pinblock, subedpan), pik);
}
}
/**
* 获得MAK
*
* @param key
* masterkey
* @param input
* 完整数据
* @param output
* mak
* @return
* @throws Exception
*/
public static byte[] mak(byte[] key, byte[] input) throws Exception {
byte[] output = new byte[8];
System.arraycopy(input, 12, output, 0, 8);
byte[] checkValue = new byte[4];
System.arraycopy(input, 20, checkValue, 0, 4);
output = decrypt(output, key);
// check valid
byte[] value = encrypt(new byte[8], output);
for (int i = 0; i < 4; i++) {
if (checkValue[i] != value[i])
throw new Exception();
}
// check over
return output;
}
/**
* 获得MAK
*
* @param key
* masterkey
* @param input
* 完整数据
* @param output
* mak
* @return
* @throws Exception
*/
public static byte[] mak16(byte[] key, byte[] input) throws Exception {
byte[] output = new byte[16];
System.arraycopy(input, 20, output, 0, 16);
byte[] checkValue = new byte[4];
System.arraycopy(input, 36, checkValue, 0, 4);
output = TriDESdecrypt(output, key);
// check valid
byte[] value = encrypt(new byte[8], output);
for (int i = 0; i < 4; i++) {
if (checkValue[i] != value[i])
throw new Exception();
}
// check over
return output;
}
/**
* 获得pik
*
* @param key
* @param input
* @param output
* @return
* @throws Exception
*/
public static byte[] pik(byte[] key, byte[] input) throws Exception {
byte[] pin = new byte[8];
System.arraycopy(input, 0, pin, 0, 8);
byte[] checkValue = new byte[4];
System.arraycopy(input, 8, checkValue, 0, 4);
pin = decrypt(pin, key);
// check valid
byte[] value = encrypt(new byte[8], pin);
for (int i = 0; i < 4; i++) {
if (checkValue[i] != value[i])
throw new Exception();
}
// check over
return pin;
}
/**
* 获得pik
*
* @param key
* @param input
* @param output
* @return
* @throws Exception
*/
public static byte[] pik16(byte[] key, byte[] input) throws Exception {
byte[] pin1 = new byte[8];
byte[] pin2 = new byte[8];
System.arraycopy(input, 0, pin1, 0, 8);
System.arraycopy(input, 8, pin2, 0, 8);
byte[] checkValue = new byte[4];
System.arraycopy(input, 16, checkValue, 0, 4);
pin1 = TriDESdecrypt(pin1, key);
pin2 = TriDESdecrypt(pin2, key);
byte[] pin = new byte[16];
System.arraycopy(pin1, 0, pin, 0, pin1.length);
System.arraycopy(pin2, 0, pin, pin1.length, pin2.length);
// check valid
byte[] value = TriDESencrypt(new byte[8], pin);
for (int i = 0; i < 4; i++) {
if (checkValue[i] != value[i])
throw new Exception();
}
// check over
return pin;
}
}