Skip to content

Instantly share code, notes, and snippets.

@yinheli
Created July 31, 2014 07:27
Show Gist options
  • Save yinheli/3b46200db9e85f354a6d to your computer and use it in GitHub Desktop.
Save yinheli/3b46200db9e85f354a6d to your computer and use it in GitHub Desktop.
银联mac算法, 示例
// 示例代码
class SampleSM {
...
public byte[] generateUnpayMACImpl(byte[] data, SecureDESKey kd)
throws SMException {
data = ISOUtil.concat(data, new byte[8]);
int len = data.length - data.length % 8;
if (data.length != len) {
data = ISOUtil.trim(data, len);
} else {
data = ISOUtil.trim(data, len - 8);
}
byte[] mac = new byte[8];
byte[] tmp = new byte[8];
byte[] block = new byte[8];
for (int i = 0; i < data.length / 8; i++) {
System.arraycopy(data, i * 8, tmp, 0, 8);
block = ISOUtil.xor(block, tmp);
}
block = ISOUtil.hexString(block).getBytes();
System.arraycopy(block, 0, tmp, 0, 8);
Key mak = decryptFromLMK(kd);
mac = this.jceHandler.encryptData(tmp, mak);
System.arraycopy(block, 8, tmp, 0, 8);
tmp = ISOUtil.xor(mac, tmp);
mac = this.jceHandler.encryptData(tmp, mak);
block = ISOUtil.hexString(mac).getBytes();
System.arraycopy(block, 0, mac, 0, 8);
return mac;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment