a) 将欲发送给POS中心的消息中,从消息类型(MTI)到63域之间的部分构成MACELEMEMENT BLOCK(MAB)。
即00-63域之间的数据块(将其拼接成字符串获取字节数组得到macBytes);
b) 对MAB,按每8个字节做异或(不管信息中的字符格式),如果最后不满8个字节,则添加“0X00”。
示例 :
MAB = M1 M2M3 M4
其中:
M1 = MS11 MS12 MS13 MS14 MS15 MS16 MS17 MS18
M2 = MS21 MS22 MS23 MS24 MS25 MS26 MS27 MS28
M3 = MS31 MS32 MS33 MS34 MS35 MS36 MS37 MS38
M4 = MS41 MS42 MS43 MS44 MS45 MS46 MS47 MS48
按如下规则进行异或运算:
MS11 MS12 MS13 MS14 MS15 MS16 MS17MS18
XOR) MS21MS22 MS23 MS24 MS25 MS26 MS27 MS28
---------------------------------------------------
TEMP BLOCK1 = TM11TM12 TM13 TM14 TM15 TM16 TM17 TM18
然后,进行下一步的运算:
TM11 TM12 TM13 TM14 TM15 TM16TM17 TM18
XOR) MS31MS32 MS33 MS34 MS35 MS36 MS37 MS38
---------------------------------------------------
TEMP BLOCK2 = TM21TM22 TM23 TM24 TM25 TM26 TM27 TM28
再进行下一步的运算:
TM21 TM22 TM23 TM24 TM25 TM26TM27 TM28
XOR) MS41MS42 MS43 MS44 MS45 MS46 MS47 MS48
---------------------------------------------------
RESULT BLOCK = TM31TM32 TM33 TM34 TM35 TM36 TM37 TM38
由于任何值与零做异或操作,都得到原值,故代码如下:
//签名字符串
byte[] tmpBytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//macBytes为 MAC原始字符串
int times = macBytes.length/8;
if (macBytes.length % 8 != 0) {
times ++;
}
for (int t = 0; t <= times; t++) {
for (int i = 0; i < 8; i++) {
if ((t*8 + i) < macBytes.length) {
tmpBytes[i] = (byte)(tmpBytes[i] ^ macBytes[t*8 + i]);
}
}
}
System.out.println("b)MAC字符串=[" + byte2hex(macBytes) + "],异或后[" + byte2hex(tmpBytes) + "]");
c) 将异或运算后的最后8个字节(RESULT BLOCK)转换成16个HEXDECIMAL:
RESULT BLOCK = TM31 TM32TM33 TM34 TM35 TM36 TM37 TM38
= TM311 TM312TM321 TM322 TM331 TM332 TM341 TM342 ||
TM351 TM352TM361 TM362 TM371 TM372 TM381 TM382
d) 取前8个字节用MAK加密:
ENC BLOCK1 = eMAK(TM311 TM312 TM321 TM322 TM331 TM332 TM341 TM342)
= EN11 EN12 EN13 EN14 EN15 EN16 EN17 EN18
e) 将加密后的结果与后8个字节异或:
EN11 EN12 EN13 EN14 EN15 EN16 EN17 EN18
XOR) TM351 TM352 TM361 TM362 TM371 TM372 TM381 TM382
------------------------------------------------------------
TEMP BLOCK= TE11 TE12 TE13 TE14 TE15 TE16 TE17 TE18
f) 用异或的结果TEMP BLOCK再进行一次单倍长密钥算法运算。
ENC BLOCK2 = eMAK(TE11 TE12 TE13 TE14 TE15 TE16 TE17 TE18)
= EN21 EN22 EN23EN24 EN25 EN26 EN27 EN28
这里我直接去前四个字节做单倍长密钥加密操作,代码如下:
//前四个字节
byte[] frontBytes = new byte[4];
//后四个字节
byte[] backBytes = new byte[4];
System.arraycopy(tmpBytes, 0, frontBytes, 0, 4);
System.arraycopy(tmpBytes, 4, backBytes, 0, 4);
try {
byte[] frontEncrypt = des(byte2hex(frontBytes).getBytes(), hex2byte(key));
System.out.println("c,d)前半段加密后结果=[" + byte2hex(frontEncrypt) + "]");
byte[] tmp = byte2hex(backBytes).getBytes();
for (int i = 0; i < 8; i++) {
tmp[i] = (byte)(frontEncrypt[i] ^ tmp[i]);
}
System.out.println("e)异或后结果=[" + byte2hex(tmp) + "]");
resultEncrypt = des(tmp, hex2byte(key));
System.out.println("f)加密结果=[" + byte2hex(resultEncrypt) + "]");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g) 将运算后的结果(ENC BLOCK2)转换成16 个HEXDECIMAL:
ENC BLOCK2 = EN21 EN22 EN23EN24 EN25 EN26 EN27 EN28
= EM211 EM212 EM221 EM222 EM231 EM232 EM241 EM242 ||
EM251 EM252 EM261 EM262 EM271 EM272 EM281EM282
示例 :
ENC RESULT= %H84, %H56, %HB1, %HCD, %H5A, %H3F, %H84, %H84
转换成16 个HEXDECIMAL:
“8456B1CD5A3F8484”
h) 取前8个字节作为MAC值。
取”8456B1CD”为MAC值。
还是先取前四个字节,然后转换成16进制大写输出,代码如下:
byte[] macResult=new byte[4];
System.arraycopy(resultEncrypt, 0, macResult, 0, 4);
System.out.println("g,h)ECB加密最终返回结果=[" + byte2hex(macResult) + "]");
return byte2hex(macResult);
附录:
单倍长密钥算法代码
public static byte[] des(byte[] reqBytes, byte[] key)
throws InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException, IllegalBlockSizeException,
BadPaddingException, UnsupportedEncodingException,
ShortBufferException {
SecretKeySpec keySpec = null;
DESKeySpec deskey = null;
deskey = new DESKeySpec(key);
keySpec = new SecretKeySpec(deskey.getKey(), "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] cipherText = new byte[cipher.getOutputSize(reqBytes.length)];
cipherText = cipher.doFinal(reqBytes);
return cipherText;
}
单倍长密钥解密代码
public static byte[] decryptByDes(byte[] reqBytes, byte[] key)
throws InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException, IllegalBlockSizeException,
BadPaddingException, UnsupportedEncodingException,
ShortBufferException {
SecretKeySpec keySpec = null;
DESKeySpec deskey = null;
deskey = new DESKeySpec(key);
keySpec = new SecretKeySpec(deskey.getKey(), "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] cipherText = new byte[cipher.getOutputSize(reqBytes.length)];
cipherText = cipher.doFinal(reqBytes);
return cipherText;
}
public static String byte2hex(byte[] b) {
StringBuffer hs = new StringBuffer();
String stmp;
for (int i = 0; i < b.length; i++) {
stmp = Integer.toHexString(b[i] & 0xFF).toUpperCase();
if (stmp.length() == 1) {
hs.append("0").append(stmp);
} else {
hs.append(stmp);
}
}
return hs.toString();
}
16进制字符串转换为10进制字节方法
public static byte[] hex2byte(String hex)
throws SecurityException {
if (hex.length() % 2 != 0) {
throw new SecurityException();
}
char[] arr = hex.toCharArray();
byte[] b = new byte[hex.length() / 2];
for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
String swap = "" + arr[i++] + arr[i];
int byteint = Integer.parseInt(swap, 16) & 0xFF;
b[j] = new Integer(byteint).byteValue();
}
return b;
}
以上,ECB算法全部结束,仅供参考!