swift 希望将EMV数据解析为TLV

zzoitvuj  于 2023-05-16  发布在  Swift
关注(0)|答案(3)|浏览(117)

好的-我有这个十六进制数据和一个标签列表:
原始数据可能如下所示:

5F201A54444320424C41434B20554E4C494D49544544205649534120204F07A00000000310105F24

因为我有一个标签列表,我可以看到这里有多个值标签[5 f20,4f,5 f24]
例如:5 F201A 54444320424 C41434 B20554 E4 C494 D49544544205649534120204F07 A00000000310105 F24ETC
所以-我知道我有这个标签[5 f20],它的值是[5 f20]和[4f]之间的所有其他东西。-
我可以拿其他的东西:
1A 54444320424 C41434 B20554 E4 C494 D4954454420564953412020并从它创建一个字符串,它变成:(“TDC黑色无限签证”)
我可以解析它的一小部分,就像使用:

let string2 = 
    "5F201A54444320424C41434B20554E4C494D49544544205649534120204F"
    var string3: String = ""
    if let startRange2 = string2.range(of: "5F20"),
    let endRange2 = string2.range(of: "4F")
    {
    let contents2 = String(string2[startRange2.upperBound..
    <endRange2.lowerBound])
    print(string3)
    string3 = contents2
    }

我不确定的是:我如何扩展它来处理那些我不知道哪个标签是第一个或第二个,或者有多少个标签的数据?我真的被难倒了,我不知道从这里去哪里。
总的来说,我的目标是创建像这样的可读数据:Clean formatted Tags and value
看起来像这样:
5F201A54444320424C41434B20554E4C494D49544544205649534120204F07A00000000310105F24032307319F160F424354455354203132334353637389F21031826509A031406179F0206000000000000 19F030600000000009F34030203009F120C56495341204352454449544F9F0607A00000000310105F300202019F4E0F616263640000000000000000000000000000C408491573FFFFFF1097C00A09117101800165E0 000AC2820168D9DE289AAD770BE408F6B1D4E0A2576CEA7F03CD479CE3A1 827375D6C4D4959ACDB5D3B6F84CD83430F4346C35E48A77A0D5F36FBEA444C2D8701C07FFC7AF06C0485D68F7A83FC30840D3C0766EC4EE66 9BE5A42BAD4C7459680FAAAE9C4EFEFFEB5A590E53B3E91B3CD2 8A415C2C9484E26DA5A15592BBCD1F45CF49D27A9D480B031957DF8C790C55FF60DB192CCD070FA4F7BCDC99E7F7567C2F991B5536F9336BA66D68115D54BC3642A9CA47FDD162FCDC33E455AAC283975DACC98CBE9A6611E996F0740072CF8E32D3D9F39F4BB25568F5CC3E7F5 DE158E4D62BF4E7185CF13BD068C4F062C26A3BBF88E056F249130E8@AA29E52A1EBB6BAD98296822F10949F0C825D1449DA7EF4431AB846D0DDB 916F2901359DD9A3B3395BAC9F9BE4D24657F65B030DDADA53577A14D9 F5F776B6FF7EAB99D8C4BB08BEF2016C72D94B1DB91BCF0 238405B7857646DCE5F79871D96B6A6652090FD8CFCC59973433919A6D0533DFE

m3eecexj

m3eecexj1#

所以你可以有三个数组。一个数组包含所有短标记,一个数组包含所有长标记,一个数组包含所有专有标记。
您可以使用创建的三个标记数组循环遍历长字符串。
首先用你的短标签数组检查长字符串,也就是前2个字符,如果它们不在你的短标签列表中,然后取前4个字符检查它是否是长标签,如果它也不在长标签列表中,那么它就是一个专有标签。
例如你的字符串,

5F201A54444320424C41434B20554E4C494D49544544205649534120204F07A00000000310105F24032307319F160F4243544553542031323334353637389F21031826509A031406179F02060000000000019F03060000000000009F34030203009F120C56495341204352454449544F9F0607A00000000310105F300202019F4E0F616263640000000000000000000000C408491573FFFFFF1097C00A09117101800165E0000AC2820168D9DE289AAD770BE408F6B1D4E0A2576CEA7F03CD479CE3A1827375D6C4D4959ACDB5D3B6F84CD83430F4346C35E48A77A0D5F36FBEA444C2D8701C07FFC7AF06C0485D68F7A83FC30840D3C0766EC4EE669BE5A42BAD4C7459680FAAAE9C4EFEFFEB5A590E53B3E91B3CD28A415C2C9484E26DA5A15592BBCD1F45CF49D27A9D480B031957DF8C790C55FF60DB192CCD070FA4F7BCDC99E7F7567C2F991B5536F9336BA66D68115D54BC3642A9CA47FDD162FCDC33E455AAC283975DACC98CBE9A6611E996F0740072CF8E32D3D9F39F4BB25568F5CC3E7F5DE158E4D62BF4E7185CF13BD068C4F062C26A3BBF88E056F249130E89AA29E52A1EBB6BAD98296822F10949F0C825D1449DA7EF4431AB846D0DDB916F2901359DD9A3B3395BAC9F9BE4D24657F65B030DDADA53577A14D9F5F776B6FF7EAB99D8C4BB08BEF2016C72D94B1DB91BCF0238405B7857646DCE5F79871D96B6A6652090FD8CFCC59973433919A6D0533DFE

你将取前两个字符5F,用它来遍历你的短标记数组,看看是否有匹配,是否有匹配。
由于没有匹配项,因此您需要重新检查它是否是一个长标记,因此长标记始终是4个字符,因此您需要使用前四个字符,即5F20。
循环遍历长标记,看看是否有5F20。是的,所以5F20后面的两个字符就是你的长度。所以1A是你的长度。我知道1A更长,因为标准格式总是
标签|长度|价值
现在你知道你的长度是多少,用这个长度来确定你的值有多少个字符。1转换成int是26,你总是把你的长度乘以2。因此,您的值将包含26 x 2 = 52个字符,从1A之后的字符开始。
因此这将是您值54444320424C41434B20554E4C494D4954454420564953412020
将十六进制转换为字符串:TDC黑色无限签证
遵循该结构将获得所有的Tag,长度和值。

wn9m85ua

wn9m85ua2#

任何EMV标签都遵循以下标签长度及其值的标准。这是从EMV Book 3中获得的。从EMVCo网站的规格部分下载。

t5zmwmid

t5zmwmid3#

我建议您使用CryptoTokenKit.framework附带的内置解码器。具体来说,TKBERTLVRecord类使用BER-TLV格式,也称为EMV。
使用内置的解码器,我们可以将TLV字符串转换为由TLV的标签键控的字典。这使得读取所有数据变得非常容易。

class EMVDecoder {
    func decode(_ inputTLV: String) -> [String: String] {
        var result: [String: String] = [:]
        if let records = TKBERTLVRecord.sequenceOfRecords(from: hexStringToData(inputTLV)) {
            for record in records {
                let hexTag = String(record.tag, radix: 16).uppercased()
                result["0x\(hexTag)"] = hexEncodedString(from: record.value)
            }
        }
        return result
    }
    
    private func hexEncodedString(from data: Data) -> String {
        return data.map { String(format: "%02X", $0) }.joined()
    }
    
    private func hexStringToData(_ hexString: String) -> Data {
        let hexString = hexString.replacingOccurrences(of: " ", with: "")
        var data = Data(capacity: hexString.count / 2)
        
        var index = hexString.startIndex
        while index < hexString.endIndex {
            let nextIndex = hexString.index(index, offsetBy: 2)
            let byteString = hexString[index..<nextIndex]
            if var byte = UInt8(byteString, radix: 16) {
                data.append(&byte, count: 1)
            }
            index = nextIndex
        }
        
        return data
    }
}

// usage

let inputString = "5F201A54444320424C41434B20554E4C494D49544544205649534120204F07A00000000310105F24032307319F160F4243544553542031323334353637389F21031826509A031406179F02060000000000019F03060000000000009F34030203009F120C56495341204352454449544F9F0607A00000000310105F300202019F4E0F616263640000000000000000000000C408491573FFFFFF1097C00A09117101800165E0000AC2820168D9DE289AAD770BE408F6B1D4E0A2576CEA7F03CD479CE3A1827375D6C4D4959ACDB5D3B6F84CD83430F4346C35E48A77A0D5F36FBEA444C2D8701C07FFC7AF06C0485D68F7A83FC30840D3C0766EC4EE669BE5A42BAD4C7459680FAAAE9C4EFEFFEB5A590E53B3E91B3CD28A415C2C9484E26DA5A15592BBCD1F45CF49D27A9D480B031957DF8C790C55FF60DB192CCD070FA4F7BCDC99E7F7567C2F991B5536F9336BA66D68115D54BC3642A9CA47FDD162FCDC33E455AAC283975DACC98CBE9A6611E996F0740072CF8E32D3D9F39F4BB25568F5CC3E7F5DE158E4D62BF4E7185CF13BD068C4F062C26A3BBF88E056F249130E89AA29E52A1EBB6BAD98296822F10949F0C825D1449DA7EF4431AB846D0DDB916F2901359DD9A3B3395BAC9F9BE4D24657F65B030DDADA53577A14D9F5F776B6FF7EAB99D8C4BB08BEF2016C72D94B1DB91BCF0238405B7857646DCE5F79871D96B6A6652090FD8CFCC59973433919A6D0533DFE"

let decodedEMV = EMVDecoder().decode(inputString)
print(decodedEMV)

/**
Outputs this dictionary:

["0xC4": "491573FFFFFF1097", "0x9F12": "56495341204352454449544F", "0x9F16": "424354455354203132333435363738", "0x9F02": "000000000001", "0x4F": "A0000000031010", "0x9F4E": "616263640000000000000000000000", "0x9F21": "182650", "0x5F30": "0201", "0x9F03": "000000000000", "0x9A": "140617", "0x9F06": "A0000000031010", "0x9F34": "020300", "0xC0": "09117101800165E0000A", "0x5F20": "54444320424C41434B20554E4C494D4954454420564953412020", "0xC2": "D9DE289AAD770BE408F6B1D4E0A2576CEA7F03CD479CE3A1827375D6C4D4959ACDB5D3B6F84CD83430F4346C35E48A77A0D5F36FBEA444C2D8701C07FFC7AF06C0485D68F7A83FC30840D3C0766EC4EE669BE5A42BAD4C7459680FAAAE9C4EFEFFEB5A590E53B3E91B3CD28A415C2C9484E26DA5A15592BBCD1F45CF49D27A9D480B031957DF8C790C55FF60DB192CCD070FA4F7BCDC99E7F7567C2F991B5536F9336BA66D68115D54BC3642A9CA47FDD162FCDC33E455AAC283975DACC98CBE9A6611E996F0740072CF8E32D3D9F39F4BB25568F5CC3E7F5DE158E4D62BF4E7185CF13BD068C4F062C26A3BBF88E056F249130E89AA29E52A1EBB6BAD98296822F10949F0C825D1449DA7EF4431AB846D0DDB916F2901359DD9A3B3395BAC9F9BE4D24657F65B030DDADA53577A14D9F5F776B6FF7EAB99D8C4BB08BEF2016C72D94B1DB91BCF0238405B7857646DCE5F79871D96B6A6652090FD8CFCC59973433919A6D0533DFE", "0x5F24": "230731"]
*/

相关问题