使用终端Sdk-2.0.3.jar上的TLVUtility从TLV中删除未知标签(C3,BF40,C2)

Remove Unknown tags (C3, BF40, C2) from TLV using TLVUtility on terminalSdk-2.0.3.jar

提问人:SHIDHIN TS 提问时间:11/8/2022 最后编辑:SHIDHIN TS 更新时间:12/6/2022 访问量:133

问:

我正在尝试使用 TLVUtility(mastercard 类)从 ByteArray 中删除一些(、、,.. 等),Tags 没有从 ByteArray 中删除。我是这样尝试的,Unknown tagsC3BF40C2terminalSdk-2.0.3.jar

这是我拥有的TLV数据,

 ppseResponse = ByteUtility.hexStringToByteArray("6F54840E325041592E5359532E4444463031C30F4920414D2054484520424553542033BF4003C20101A52BBF0C28610C4F07A0000000041010870101610C4F07A0000000043060870103610A4F05B0123456788701099000")

我正在尝试删除类似的东西,Unknown tags

"BEFORE << ${ByteUtility.byteArrayToHexString(ppseResponse)}".toLog(activity, TAG)  //To print Log

val unknownTags = byteArrayOf((0x00C3).toByte(),(0xBF40).toByte(), (0x00C2).toByte())

var removed = TLVUtility.removeTag(ppseResponse, ContentType.TLV,unknownTags)

"AFTER << ${ByteUtility.byteArrayToHexString(removed)}".toLog(activity, TAG) //To print Log

这是函数上的函数,如下所示,removeTagterminalSdk-2.0.3.jar

public static byte[] removeTag(byte[] var0, ContentType var1, byte[] var2) {
        int var10000;
        Throwable var10001;
        String var28;
        label698: {
            var10000 = 2 % 2;
            if (var0 != null) {
                label691: {
                    var10000 = g + 85;
                    f = var10000 % 128;
                    if (var10000 % 2 == 0) {
                        byte[] var26 = var2;

                        byte var27;
                        label676: {
                            label675: {
                                try {
                                    null.hashCode();
                                    if (var26 == null) {
                                        break label675;
                                    }
                                } catch (Throwable var25) {
                                    throw var25;
                                }

                                var27 = 42;
                                break label676;
                            }

                            var27 = 74;
                        }

                        switch(var27) {
                        case 42:
                            break;
                        case 74:
                        default:
                            break label691;
                        }
                    } else if (var2 == null) {
                        break label691;
                    }

                    var10000 = f + 55;
                    g = var10000 % 128;
                    switch(var10000 % 2 != 0 ? 0 : 1) {
                    case 0:
                    default:
                        if (var2.length >= 1) {
                            break label698;
                        }
                        break;
                    case 1:
                        if (var2.length >= 1) {
                            break label698;
                        }
                    }
                }
            }

            var28 = e(31, '卆', 490).intern();

            try {
                ((Class)q.b(38, 8595, '\ue0f7')).getMethod("e", String.class).invoke((Object)null, var28);
            } catch (Throwable var22) {
                var10001 = var22.getCause();
                if (var10001 != null) {
                    throw var10001;
                }

                throw var22;
            }

            throw new LibraryUncheckedException(ExceptionCode.X6F44);
        }

        label700: {
            if (var1 == ContentType.T) {
                var10000 = g + 15;
                f = var10000 % 128;
                if (var10000 % 2 == 0) {
                    if (var0.length < 0) {
                        break label700;
                    }
                } else if (var0.length < 1) {
                    break label700;
                }
            }

            switch(var1 != ContentType.TLV ? 81 : 64) {
            case 81:
                if (var1 != ContentType.TDO) {
                    break;
                }
            case 64:
            default:
                if (var0.length < 3) {
                    var28 = e(47, '\u0000', 560).intern();

                    try {
                        ((Class)q.b(38, 8595, '\ue0f7')).getMethod("e", String.class).invoke((Object)null, var28);
                    } catch (Throwable var23) {
                        var10001 = var23.getCause();
                        if (var10001 != null) {
                            throw var10001;
                        }

                        throw var23;
                    }

                    throw new LibraryUncheckedException(ExceptionCode.X6F43);
                }
            }

            ArrayList var3 = conditionalTlvParsing(var0, var1, e(1, '\u0000', 140).intern(), true);
            Iterator var4 = var3.iterator();

            while(var4.hasNext()) {
                BerTlv var5 = (BerTlv)var4.next();
                switch(var5.getTagObject().getNTag() == ByteUtility.byteArrayToInt(var2) ? 39 : 84) {
                case 39:
                default:
                    var4.remove();
                case 84:
                }
            }

            return formByteArrayFromTlvList(var3, var1);
        }

        var28 = e(39, '\u0000', 521).intern();

        try {
            ((Class)q.b(38, 8595, '\ue0f7')).getMethod("e", String.class).invoke((Object)null, var28);
        } catch (Throwable var24) {
            var10001 = var24.getCause();
            if (var10001 != null) {
                throw var10001;
            }

            throw var24;
        }

        throw new LibraryUncheckedException(ExceptionCode.X6F43);
    }

    
Android Kotlin EMV 万事达卡 TLV

评论

1赞 Ivo 11/8/2022
是什么样子的?TLVUtility.removeTag
0赞 SHIDHIN TS 11/8/2022
@Ivo更新的问题,请检查一下
0赞 Ivo 11/8/2022
好吧,这看起来超出了我的专业知识。我希望别人能帮助你
0赞 user3738870 11/8/2022
这个“TLVUtility(万事达卡类)”是从哪里来的?您粘贴的代码看起来像是从 Java 字节码反编译的,而不是实际代码。
0赞 SHIDHIN TS 11/8/2022
从 terminalSdk-2.0.3 @user3738870它.jar

答:

0赞 SHIDHIN TS 11/13/2022 #1

我通过创建新的自定义函数解决了这个问题。这是函数。现在在这个函数上,我正在删除所有及其跳过所有和和标签Unknown TagsKnown TagsThird party dataOptional Tags

首先我们需要定义可选的 Tag,

private var optionalTags: ArrayList<ByteArray> = getOptionalTags()

这是我添加可选标签的功能,

private fun getOptionalTags(): ArrayList<ByteArray> {
        optionalTags = ArrayList()
        optionalTags.add(ByteUtility.hexStringToByteArray("9F3E"))   //Terminal Categories Supported List
        optionalTags.add(ByteUtility.hexStringToByteArray("9F3F"))   //Selection Data Object List
        optionalTags.add(ByteUtility.hexStringToByteArray("9F2A"))   //Kernel Identifier
        optionalTags.add(ByteUtility.hexStringToByteArray("9F0A"))   //Application Selection Registered Proprietary Data
        return optionalTags
    }

这是删除未知标签的功能,

 private fun removeUnknownTags(ppseResponse: ByteArray): ByteArray {
        var parentTags: MutableMap<ByteArray, Int> = LinkedHashMap()

        var ppseResp = ppseResponse;
        var i = 0
        "PPSE (Length: ${ppseResp.size}) : HexString: ${ByteUtility.byteArrayToHexString(ppseResp)} \n ByteArray: ${ppseResp.toList()}".toLog()
        while (i < ppseResp.size - 1) {
            var removeTag = false;
            var currentTag = TLVUtility.getCurrentTag(ppseResp, i);
            var nextIndex = i + currentTag.size;
            var tLVLengthInfo = TLVUtility.getTLVLengthInfo(ppseResp, nextIndex);
            val tagLength = tLVLengthInfo.expectedNumberOfBytes
            var tagValueSize = tLVLengthInfo.lengthBytes.size
            var tagValue = ByteUtility.getSubByteArray(ppseResp, nextIndex + tagValueSize, tagLength)
            "<==- ${i} :  ${ByteUtility.byteArrayToHexString(currentTag)} (${TLVUtility.getReferenceTagDetails(currentTag).name}) , Length:${tagLength} (${tLVLengthInfo.lengthBytes.toList()} : ${ByteUtility.byteArrayToHexString(tLVLengthInfo.lengthBytes)}) , Max:${TLVUtility.getReferenceTagDetails(currentTag).maxLen} , Min:${TLVUtility.getReferenceTagDetails(currentTag).minLen} -==>".toLog()
            "Value  : ${ByteUtility.byteArrayToHexString(tagValue)} , Bytes:${tagValue.toList()}".toLog()
            var tagNameLengthValue = ByteUtility.getSubByteArray(ppseResp, i, currentTag.size + tagValueSize + tagLength)

            if (TLVUtility.getReferenceTagDetails(currentTag).name.contentEquals("Unknown Tag")) {
                var optionalTag = false;
                for (optTag in optionalTags) {
                    if (currentTag.contentEquals(optTag)) {
                        "${ByteUtility.byteArrayToHexString(currentTag)} is Optional Tag".toLog()
                        i = nextIndex + tagValueSize + tagLength;
                        optionalTag = true;
                        break
                    }
                }
                if (!optionalTag)
                    removeTag = true
            } else if (TLVUtility.getReferenceTagDetails(currentTag).name.contentEquals("Third Party Data")) {
                i = nextIndex + tagValueSize + tagLength;
            } else {
                if (!removeTag) {
                    if (TLVUtility.isTLVEncoded(tagValue)) {
                        parentTags.put(currentTag, i)
                        parentTags.entries.stream()
                            .sorted(Map.Entry.comparingByValue<ByteArray, Int>().reversed())
                            .forEachOrdered { x -> parentTags.put(x.key, x.value) }
                        if (tagValue.contentEquals(ByteUtility.hexStringToByteArray("00"))) {
                           i = nextIndex + tagValueSize + tagLength;
                        } else
                            i = nextIndex + tagValueSize
                    } else {
                        i = nextIndex + tagValueSize + tagLength;
                    }
                }
            }

            if (removeTag) {
                "-*-*-*-Removing TAG ${ByteUtility.byteArrayToHexString(currentTag)}-*-*-*-".toLog()
                ppseResp = ByteUtility.getSubByteArray(ppseResp, 0, i) + ByteUtility.getSubByteArray(ppseResp, i + tagNameLengthValue.size)
                var byteDiff = tagNameLengthValue.size

                "--Updating Tag Length (Removed Bytes ${byteDiff})--".toLog()
                for (tag in parentTags.entries.reversed()) {
                    var parentBytes = tag.key
                    var index = tag.value
                    var sizeIndex = index + parentBytes.size
                    var unknownTLVLengthInfo = TLVUtility.getTLVLengthInfo(ppseResp, sizeIndex);
                    val length = unknownTLVLengthInfo.expectedNumberOfBytes
                    if (i > index && i <= index + length) {  //To Check is Parent Tag
                        "Parent Tag ${ByteUtility.byteArrayToHexString(tag.key)} (Bytes:${tag.key.toList()}) , Position:${tag.value}".toLog()
                        var lengthByteArray = unknownTLVLengthInfo.lengthBytes
                        var newLengthByteArray = ByteUtility.intToBerEncodedLength(length - byteDiff).bytes
                        var lengthDiff = lengthByteArray.size - newLengthByteArray.size;
                        byteDiff = byteDiff + lengthDiff
                        ppseResp = ByteUtility.getSubByteArray(ppseResp, 0, sizeIndex) + newLengthByteArray + ByteUtility.getSubByteArray(ppseResp, sizeIndex + lengthByteArray.size)
                        if (lengthDiff > 0) {
                            i = i - lengthDiff
                            for (child in parentTags.entries) {
                                if (child.value > tag.value) {
                                   parentTags.replace(child.key, child.value - lengthDiff)
                                }
                            }
                        }
                        // "PPSE After (Length: ${ppseResp.size}):  ${ByteUtility.byteArrayToHexString(ppseResp)} \n ByteArray: ${ppseResp.toList()}".toLog()
                    }
                }
            }

        }
        return ppseResp;
    }