为什么 ISO 8583 解包会产生无效值?

Why ISO 8583 unpacking produces invalid values?

我需要解压并获取 MTI,当前数据元素值为

303831308220000002000000040000000000000031313233303930323333303733313536303033303

1 消息字符串。正如您所见,MTI 应该是 3038。但是当我使用 isoMsg.getMTI(); 语句获取 MTI 值时,它给出 0810 作为 output.So 我的代码有什么问题?请参阅本教程。https://kodejava.org/how-do-i-unpack-an-iso-8583-message/ 其中前四个字段作为 MTI,但我没有将前四个字段作为 MTI。请提供详细的答案。

public class IsoUnpacker {

    public static void main(String[] args) {
        String message = "3038313082200000020000000400000000000000313132333039303233333037333135363030333031";

        IsoUnpacker iso = new IsoUnpacker();
        try {
            ISOMsg isoMsg = iso.parseISOMessage(message);
            iso.printISOMessage(isoMsg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private ISOMsg parseISOMessage(String message) throws Exception {

        System.out.printf("Message = %s%n", message);
        try {


            ISOMsg isoMsg= new ISOMsg();

            GenericValidatingPackager packager  = new GenericValidatingPackager();
            packager.readFile("C:\Users\Hasindu\Documents\ISO_Unpacker\iso_unpacking\src\resources\fields.xml");

            isoMsg.setPackager(packager);
            isoMsg.unpack(ISOUtil.hex2byte(message));


            return isoMsg;
        } catch (ISOException e) {
            System.out.println(e.getMessage());
        }
        return null ;
    }

    private void printISOMessage(ISOMsg isoMsg) {
        try {
            System.out.printf("MTI = %s%n", isoMsg.getMTI());
            for (int i = 1; i <= isoMsg.getMaxField(); i++) {
                if (isoMsg.hasField(i)) {
                    System.out.printf("Field (%s) = %s%n", i, isoMsg.getString(i));
                }
            }
        } catch (ISOException e) {
            e.printStackTrace();
        }
    }
}

fields.xml 文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE isopackager PUBLIC
        "-//jPOS/jPOS Generic Packager DTD 1.0//EN"
        "http://jpos.org/dtd/generic-packager-1.0.dtd">

<!-- ISO 8583:1987 (ASCII) field descriptions for GenericPackager -->
<!-- Bitmap is BINARY in this case -->

<isopackager>
    <isofield
            id="0"
            length="4"
            name="MESSAGE TYPE INDICATOR"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="1"
            length="16"
            name="BIT MAP"
            class="org.jpos.iso.IFB_BITMAP"/>
    <isofield
            id="2"
            length="19"
            name="PAN - PRIMARY ACCOUNT NUMBER"
            class="org.jpos.iso.IFA_LLNUM"/>
    <isofield
            id="3"
            length="6"
            name="PROCESSING CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="4"
            length="12"
            name="AMOUNT, TRANSACTION"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="5"
            length="12"
            name="AMOUNT, SETTLEMENT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="6"
            length="12"
            name="AMOUNT, CARDHOLDER BILLING"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="7"
            length="10"
            name="TRANSMISSION DATE AND TIME"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="8"
            length="8"
            name="AMOUNT, CARDHOLDER BILLING FEE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="9"
            length="8"
            name="CONVERSION RATE, SETTLEMENT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="10"
            length="8"
            name="CONVERSION RATE, CARDHOLDER BILLING"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="11"
            length="6"
            name="SYSTEM TRACE AUDIT NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="12"
            length="6"
            name="TIME, LOCAL TRANSACTION"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="13"
            length="4"
            name="DATE, LOCAL TRANSACTION"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="14"
            length="4"
            name="DATE, EXPIRATION"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="15"
            length="4"
            name="DATE, SETTLEMENT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="16"
            length="4"
            name="DATE, CONVERSION"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="17"
            length="4"
            name="DATE, CAPTURE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="18"
            length="4"
            name="MERCHANTS TYPE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="19"
            length="3"
            name="ACQUIRING INSTITUTION COUNTRY CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="20"
            length="3"
            name="PAN EXTENDED COUNTRY CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="21"
            length="3"
            name="FORWARDING INSTITUTION COUNTRY CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="22"
            length="3"
            name="POINT OF SERVICE ENTRY MODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="23"
            length="3"
            name="CARD SEQUENCE NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="24"
            length="3"
            name="NETWORK INTERNATIONAL IDENTIFIEER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="25"
            length="2"
            name="POINT OF SERVICE CONDITION CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="26"
            length="2"
            name="POINT OF SERVICE PIN CAPTURE CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="27"
            length="1"
            name="AUTHORIZATION IDENTIFICATION RESP LEN"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="28"
            length="9"
            name="AMOUNT, TRANSACTION FEE"
            class="org.jpos.iso.IFA_AMOUNT"/>
    <isofield
            id="29"
            length="9"
            name="AMOUNT, SETTLEMENT FEE"
            class="org.jpos.iso.IFA_AMOUNT"/>
    <isofield
            id="30"
            length="9"
            name="AMOUNT, TRANSACTION PROCESSING FEE"
            class="org.jpos.iso.IFA_AMOUNT"/>
    <isofield
            id="31"
            length="9"
            name="AMOUNT, SETTLEMENT PROCESSING FEE"
            class="org.jpos.iso.IFA_AMOUNT"/>
    <isofield
            id="32"
            length="11"
            name="ACQUIRING INSTITUTION IDENT CODE"
            class="org.jpos.iso.IFA_LLNUM"/>
    <isofield
            id="33"
            length="11"
            name="FORWARDING INSTITUTION IDENT CODE"
            class="org.jpos.iso.IFA_LLNUM"/>
    <isofield
            id="34"
            length="28"
            name="PAN EXTENDED"
            class="org.jpos.iso.IFA_LLCHAR"/>
    <isofield
            id="35"
            length="37"
            name="TRACK 2 DATA"
            class="org.jpos.iso.IFA_LLNUM"/>
    <isofield
            id="36"
            length="104"
            name="TRACK 3 DATA"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="37"
            length="12"
            name="RETRIEVAL REFERENCE NUMBER"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="38"
            length="6"
            name="AUTHORIZATION IDENTIFICATION RESPONSE"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="39"
            length="2"
            name="RESPONSE CODE"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="40"
            length="3"
            name="SERVICE RESTRICTION CODE"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="41"
            length="8"
            name="CARD ACCEPTOR TERMINAL IDENTIFICACION"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="42"
            length="15"
            name="CARD ACCEPTOR IDENTIFICATION CODE"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="43"
            length="40"
            name="CARD ACCEPTOR NAME/LOCATION"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="44"
            length="25"
            name="ADITIONAL RESPONSE DATA"
            class="org.jpos.iso.IFA_LLCHAR"/>
    <isofield
            id="45"
            length="76"
            name="TRACK 1 DATA"
            class="org.jpos.iso.IFA_LLCHAR"/>
    <isofield
            id="46"
            length="999"
            name="ADITIONAL DATA - ISO"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="47"
            length="999"
            name="ADITIONAL DATA - NATIONAL"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="48"
            length="999"
            name="ADITIONAL DATA - PRIVATE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="49"
            length="3"
            name="CURRENCY CODE, TRANSACTION"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="50"
            length="3"
            name="CURRENCY CODE, SETTLEMENT"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="51"
            length="3"
            name="CURRENCY CODE, CARDHOLDER BILLING"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="52"
            length="8"
            name="PIN DATA"
            class="org.jpos.iso.IFA_BINARY"/>
    <isofield
            id="53"
            length="16"
            name="SECURITY RELATED CONTROL INFORMATION"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="54"
            length="120"
            name="ADDITIONAL AMOUNTS"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="55"
            length="999"
            name="RESERVED ISO"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="56"
            length="999"
            name="RESERVED ISO"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="57"
            length="999"
            name="RESERVED NATIONAL"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="58"
            length="999"
            name="RESERVED NATIONAL"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="59"
            length="999"
            name="RESERVED NATIONAL"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="60"
            length="999"
            name="RESERVED PRIVATE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="61"
            length="999"
            name="RESERVED PRIVATE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="62"
            length="999"
            name="RESERVED PRIVATE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="63"
            length="999"
            name="RESERVED PRIVATE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="64"
            length="8"
            name="MESSAGE AUTHENTICATION CODE FIELD"
            class="org.jpos.iso.IFA_BINARY"/>
    <isofield
            id="65"
            length="1"
            name="BITMAP, EXTENDED"
            class="org.jpos.iso.IFA_BINARY"/>
    <isofield
            id="66"
            length="1"
            name="SETTLEMENT CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="67"
            length="2"
            name="EXTENDED PAYMENT CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="68"
            length="3"
            name="RECEIVING INSTITUTION COUNTRY CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="69"
            length="3"
            name="SETTLEMENT INSTITUTION COUNTRY CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="70"
            length="3"
            name="NETWORK MANAGEMENT INFORMATION CODE"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="71"
            length="4"
            name="MESSAGE NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="72"
            length="4"
            name="MESSAGE NUMBER LAST"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="73"
            length="6"
            name="DATE ACTION"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="74"
            length="10"
            name="CREDITS NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="75"
            length="10"
            name="CREDITS REVERSAL NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="76"
            length="10"
            name="DEBITS NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="77"
            length="10"
            name="DEBITS REVERSAL NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="78"
            length="10"
            name="TRANSFER NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="79"
            length="10"
            name="TRANSFER REVERSAL NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="80"
            length="10"
            name="INQUIRIES NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="81"
            length="10"
            name="AUTHORIZATION NUMBER"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="82"
            length="12"
            name="CREDITS, PROCESSING FEE AMOUNT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="83"
            length="12"
            name="CREDITS, TRANSACTION FEE AMOUNT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="84"
            length="12"
            name="DEBITS, PROCESSING FEE AMOUNT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="85"
            length="12"
            name="DEBITS, TRANSACTION FEE AMOUNT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="86"
            length="16"
            name="CREDITS, AMOUNT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="87"
            length="16"
            name="CREDITS, REVERSAL AMOUNT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="88"
            length="16"
            name="DEBITS, AMOUNT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="89"
            length="16"
            name="DEBITS, REVERSAL AMOUNT"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="90"
            length="42"
            name="ORIGINAL DATA ELEMENTS"
            class="org.jpos.iso.IFA_NUMERIC"/>
    <isofield
            id="91"
            length="1"
            name="FILE UPDATE CODE"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="92"
            length="2"
            name="FILE SECURITY CODE"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="93"
            length="6"
            name="RESPONSE INDICATOR"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="94"
            length="7"
            name="SERVICE INDICATOR"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="95"
            length="42"
            name="REPLACEMENT AMOUNTS"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="96"
            length="16"
            name="MESSAGE SECURITY CODE"
            class="org.jpos.iso.IFA_BINARY"/>
    <isofield
            id="97"
            length="17"
            name="AMOUNT, NET SETTLEMENT"
            class="org.jpos.iso.IFA_AMOUNT"/>
    <isofield
            id="98"
            length="25"
            name="PAYEE"
            class="org.jpos.iso.IF_CHAR"/>
    <isofield
            id="99"
            length="11"
            name="SETTLEMENT INSTITUTION IDENT CODE"
            class="org.jpos.iso.IFA_LLNUM"/>
    <isofield
            id="100"
            length="11"
            name="RECEIVING INSTITUTION IDENT CODE"
            class="org.jpos.iso.IFA_LLNUM"/>
    <isofield
            id="101"
            length="17"
            name="FILE NAME"
            class="org.jpos.iso.IFA_LLCHAR"/>
    <isofield
            id="102"
            length="28"
            name="ACCOUNT IDENTIFICATION 1"
            class="org.jpos.iso.IFA_LLCHAR"/>
    <isofield
            id="103"
            length="28"
            name="ACCOUNT IDENTIFICATION 2"
            class="org.jpos.iso.IFA_LLCHAR"/>
    <isofield
            id="104"
            length="100"
            name="TRANSACTION DESCRIPTION"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="105"
            length="999"
            name="RESERVED ISO USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="106"
            length="999"
            name="RESERVED ISO USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="107"
            length="999"
            name="RESERVED ISO USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="108"
            length="999"
            name="RESERVED ISO USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="109"
            length="999"
            name="RESERVED ISO USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="110"
            length="999"
            name="RESERVED ISO USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="111"
            length="999"
            name="RESERVED ISO USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="112"
            length="999"
            name="RESERVED NATIONAL USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="113"
            length="999"
            name="RESERVED NATIONAL USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="114"
            length="999"
            name="RESERVED NATIONAL USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="115"
            length="999"
            name="RESERVED NATIONAL USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="116"
            length="999"
            name="RESERVED NATIONAL USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="117"
            length="999"
            name="RESERVED NATIONAL USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="118"
            length="999"
            name="RESERVED NATIONAL USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="119"
            length="999"
            name="RESERVED NATIONAL USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="120"
            length="999"
            name="RESERVED PRIVATE USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="121"
            length="999"
            name="RESERVED PRIVATE USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="122"
            length="999"
            name="RESERVED PRIVATE USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="123"
            length="999"
            name="RESERVED PRIVATE USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="124"
            length="999"
            name="RESERVED PRIVATE USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="125"
            length="999"
            name="RESERVED PRIVATE USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="126"
            length="999"
            name="RESERVED PRIVATE USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="127"
            length="999"
            name="RESERVED PRIVATE USE"
            class="org.jpos.iso.IFA_LLLCHAR"/>
    <isofield
            id="128"
            length="8"
            name="MAC 2"
            class="org.jpos.iso.IFA_BINARY"/>
</isopackager>

输出:

Message = 3038313082200000020000000400000000000000313132333039303233333037333135363030333031
MTI = 0810
Field (7) = 1123090233
Field (11) = 073156
Field (39) = 00
Field (70) = 301

这不是一个完整的答案,但对于评论来说太大了,可能会帮助您找出问题所在。

注:

3038313082200000020000000400000000000000313132333039303233333037333135363030333031

这似乎是一个十六进制编码的 ASCII 字符串,开头附近有一些二进制内容

如果我转换为 ASCII,同时猜测二进制位是 32 位小端整数,我得到

"0810" ASCII
0x00002082 
0x00000002
0x00000004
0x00000000
"112309023307315600301" ASCII

希望以上内容能帮助您解决问题。

这是一条有效且格式正确的消息。 MTI 0810 = Network Management Response, field 70, 网管信息代码, 301表示echo test, field 39 = 00表示OK, field 11是系统跟踪审计号,一个消息的id,将响应绑定到请求上,字段 7 是日期和时间,11 月 23 日,09:02:33.

位图为82200000020000000400000000000000。从1开始计数,分别为1、7、11、39、70。字段1为第二个位图。

ISO 8583 是一个相当宽松的标准,每个实现都略有不同。例如,每个卡片品牌都有自己的版本。但是这个特殊的例子是标准的。

我没有将它与您提供的参考资料进行比较,而是与标准版本进行比较。

试试这个:

package org.kodejava.example.jpos;

import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.packager.GenericPackager;

import java.io.InputStream;

public class IsoUnpacker {
    public static void main(String[] args) {
        IsoUnpacker iso = new IsoUnpacker();
        try {
        String message = "02003220000000808000000010000000001500120604120000000112340001840";
            ISOMsg isoMsg = iso.parseISOMessage(message);
            iso.printISOMessage(isoMsg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private ISOMsg parseISOMessage(String message) throws Exception {
        System.out.printf("Message = %s%n", message);
        try {
            // Load package from resources directory.
            InputStream is = getClass().getResourceAsStream("C:\Users\Hasindu\Documents\ISO_Unpacker\iso_unpacking\src\resources\fields.xm");
            GenericPackager packager = new GenericPackager(is);
            ISOMsg isoMsg = new ISOMsg();
            isoMsg.setPackager(packager);
            isoMsg.unpack(message.getBytes());
            return isoMsg;
        } catch (ISOException e) {
            throw new Exception(e);
        }
    }

    private void printISOMessage(ISOMsg isoMsg) {
        try {
            System.out.printf("MTI = %s%n", isoMsg.getMTI());
            for (int i = 1; i <= isoMsg.getMaxField(); i++) {
                if (isoMsg.hasField(i)) {
                    System.out.printf("Field (%s) = %s%n", i, isoMsg.getString(i));
                }
            }
        } catch (ISOException e) {
            e.printStackTrace();
        }
    }
}

在您的方法中一切都很好,每个人都尝试过解释相同的内容,但请允许我为您澄清一下基础知识,然后将尝试回答您在 OP 或评论中提出的所有问题。

Understand ISO 8583 (Disclaimer : my words & from Wikipedia)

它是金融交易消息传递的国际标准。它带有定义的格式,许多大型计划(如 MasterCard、Visa)都使用它。 根据定义,前 4 位是 MTI -> Message Type Indicator。依此类推,数字及其位置具有明确的含义。

基本定义这是什么类型的消息? 现在,如前所述,这是一个标准协议,甚至 定义了有效的 MTI。因此,在学习或理解 ISO 8583 时,您应该开始学习什么是有效的 MTI?

现在我强烈建议在进一步阅读之前先阅读本节:MTI_Wiki

看完你可能已经明白了——消息类型指示符包括ISO 8583版本、消息Class、消息函数和消息来源。 即:这 4 位数字解释了以上 3 件事。

以您的消息为例:303831xxxxxxxxxxxxxxxxxxxxxx

现在,如果我们按照您的消息并查看我上面分享的 link,第一个 数字 3 3038xx根据 ISO 8583 中的内容无法使用,而是由 ISO 本身保留。

这意味着 - 您最初期望的 MTI 3038 - 甚至不是有效的 MTI !! 这进一步应该让你意识到这是一条错误的消息或通过某种方式编码的消息。

在你的情况下是后者,因为它是十六进制编码的消息,完全可以,因为许多业务实体可以选择任何类型的协议。

Having said that, you must understand in order to use any ISO library to pack or unpack or anything else you must first translate your message in valid ISO 8583 format.

所以你决定使用 isoMsg.unpack(ISOUtil.hex2byte(message)); 在这种情况下是绝对有效的 当然这个 isoMsg.unpack(message.getBytes()); 肯定会失败,因为当你说 message.getBytes() 您没有将有效的 ISO 8583 消息传递给标准解包函数。

所以在您的消息中:

Message = 3038313082200000020000000400000000000000313132333039303233333037333135363030333031

30383130(十六进制编码)是 MTI = 0810

How to know if message is base-16 (Hex encoded)?

为此,强烈建议与发起者达成协议 acquirer/terminal/anyInterface 让 consumer/bank/anyInterface 知道消息将以何种格式到达。

这些是整个组织的标准做法,然后以这种方式配置响应程序。而且我敢肯定,从您收到 303831xxxxxxxxxx 的发起者那里,明天您将不会开始收到 0810xxxx(就像在 ASCII 中一样),除非他们在 configuration/set 最后更改 you/your ] 必须通知公司。对于成功的金融交易,所有具有约束力的各方都可以发挥作用。

我无法回忆起任何库来检测编码,当然,你可以有一些可以检测的东西,但我仍然会要求像我上面所说的那样将其标准化为双方之间的协议。

专业提示: 随着时间的推移,您自己将能够通过查看 MTI 来判断编码或消息的类型 ;) !相信我!

希望我回答了你所有的疑惑,如果还有的话,请不要犹豫。 祝你好运!