JPOS ISO 8583 解析问题
JPOS ISO 8583 parsing issue
我用 ISO 8583 v87 创建了一条消息!但是在解析它时,我得到了不同的参数值。问题是什么?是为了 Hex.encodeHexString
import org.apache.commons.codec.binary.Hex;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
public class ParseISOMessage {
static org.jpos.iso.packager.ISO87APackager packager;
public static void main(String[] args) throws IOException, ISOException {
String data = createMSG();
// Create Packager based on XML that contain DE type
packager = new org.jpos.iso.packager.ISO87APackager();
// Create ISO Message
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
isoMsg.unpack(data.getBytes());
// print the DE list
logISOMsg(isoMsg);
}
private static void logISOMsg(ISOMsg msg) {
System.out.println("----ISO MESSAGE-----");
try {
System.out.println(" MTI : " + msg.getMTI());
for (int i=1;i<=msg.getMaxField();i++) {
if (msg.hasField(i)) {
System.out.println(" Field-"+i+" ("+packager.getFieldPackager(i).getDescription()+") : "+msg.getString(i));
}
}
} catch (ISOException e) {
e.printStackTrace();
} finally {
System.out.println("--------------------");
}
}
private static String createMSG() {
try{
ISO87BPackager packager = new ISO87BPackager();
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
isoMsg.setMTI("0200");
isoMsg.set(2, "1234567890123456");
isoMsg.set(3, "000000");
isoMsg.set(4,"000000001234");
isoMsg.set(7, "0413135553");
isoMsg.set(11, "000091");
isoMsg.set(12, "135553");
isoMsg.set(13, "0413");
isoMsg.set(25, "14");
isoMsg.set(32, "5816720610");
isoMsg.set(35, "1234567890123456=9505146000000000000");
isoMsg.set(37, "000000000000");
isoMsg.set(41,"55555555");
isoMsg.set(49,"123");
isoMsg.set(52,"1A2B3C4D5E6F7012");
isoMsg.set(64,"ABCDEF0123456789");
byte[] b = isoMsg.pack();
System.out.println("**"+Hex.encodeHexString(b)+"**");
return Hex.encodeHexString(b);
}
catch(Exception ex) {
ex.printStackTrace();
return "";
}
}
}
例如,我将PIN DATA设置为ABCDEF0123456789
,但是在解析创建的消息时,PIN DATA是0353535353535353
.
当您调用 data.getBytes()
时,问题很简单,数据实际上是编码的十六进制数据,而不是 isoMsg.pack()
创建的数据。
更改 isoMsg.unpack(data.getBytes())
到
final byte[] bytes = new Hex().decode(data.getBytes());
isoMsg.unpack(bytes);
你也应该使用相同的packager
打包和解包
public class ParseISOMessage {
private static org.jpos.iso.packager.ISO87APackager packager = new org.jpos.iso.packager.ISO87APackager();
public static void main(String[] args) throws IOException, ISOException, DecoderException {
String data = createMSG();
// Create ISO Message
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
final byte[] bytes = new Hex().decode(data.getBytes());
isoMsg.unpack(bytes);
...
private static String createMSG() {
try {
// ISO87BPackager packager = new ISO87BPackager(); // delete this line otherwise it cannot unpack
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
isoMsg.setMTI("0200");
...//same
isoMsg.set(64, "ABCDEF0123456789");
byte[] b = isoMsg.pack();
System.out.println("**" + new String(Hex.encodeHex(b, false)) + "**"); // make the result uppercase.
return Hex.encodeHexString(b);
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
}
我用 ISO 8583 v87 创建了一条消息!但是在解析它时,我得到了不同的参数值。问题是什么?是为了 Hex.encodeHexString
import org.apache.commons.codec.binary.Hex;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
public class ParseISOMessage {
static org.jpos.iso.packager.ISO87APackager packager;
public static void main(String[] args) throws IOException, ISOException {
String data = createMSG();
// Create Packager based on XML that contain DE type
packager = new org.jpos.iso.packager.ISO87APackager();
// Create ISO Message
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
isoMsg.unpack(data.getBytes());
// print the DE list
logISOMsg(isoMsg);
}
private static void logISOMsg(ISOMsg msg) {
System.out.println("----ISO MESSAGE-----");
try {
System.out.println(" MTI : " + msg.getMTI());
for (int i=1;i<=msg.getMaxField();i++) {
if (msg.hasField(i)) {
System.out.println(" Field-"+i+" ("+packager.getFieldPackager(i).getDescription()+") : "+msg.getString(i));
}
}
} catch (ISOException e) {
e.printStackTrace();
} finally {
System.out.println("--------------------");
}
}
private static String createMSG() {
try{
ISO87BPackager packager = new ISO87BPackager();
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
isoMsg.setMTI("0200");
isoMsg.set(2, "1234567890123456");
isoMsg.set(3, "000000");
isoMsg.set(4,"000000001234");
isoMsg.set(7, "0413135553");
isoMsg.set(11, "000091");
isoMsg.set(12, "135553");
isoMsg.set(13, "0413");
isoMsg.set(25, "14");
isoMsg.set(32, "5816720610");
isoMsg.set(35, "1234567890123456=9505146000000000000");
isoMsg.set(37, "000000000000");
isoMsg.set(41,"55555555");
isoMsg.set(49,"123");
isoMsg.set(52,"1A2B3C4D5E6F7012");
isoMsg.set(64,"ABCDEF0123456789");
byte[] b = isoMsg.pack();
System.out.println("**"+Hex.encodeHexString(b)+"**");
return Hex.encodeHexString(b);
}
catch(Exception ex) {
ex.printStackTrace();
return "";
}
}
}
例如,我将PIN DATA设置为ABCDEF0123456789
,但是在解析创建的消息时,PIN DATA是0353535353535353
.
当您调用 data.getBytes()
时,问题很简单,数据实际上是编码的十六进制数据,而不是 isoMsg.pack()
创建的数据。
更改 isoMsg.unpack(data.getBytes())
到
final byte[] bytes = new Hex().decode(data.getBytes());
isoMsg.unpack(bytes);
你也应该使用相同的packager
打包和解包
public class ParseISOMessage {
private static org.jpos.iso.packager.ISO87APackager packager = new org.jpos.iso.packager.ISO87APackager();
public static void main(String[] args) throws IOException, ISOException, DecoderException {
String data = createMSG();
// Create ISO Message
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
final byte[] bytes = new Hex().decode(data.getBytes());
isoMsg.unpack(bytes);
...
private static String createMSG() {
try {
// ISO87BPackager packager = new ISO87BPackager(); // delete this line otherwise it cannot unpack
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
isoMsg.setMTI("0200");
...//same
isoMsg.set(64, "ABCDEF0123456789");
byte[] b = isoMsg.pack();
System.out.println("**" + new String(Hex.encodeHex(b, false)) + "**"); // make the result uppercase.
return Hex.encodeHexString(b);
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
}