TLV encoding and decoding of PBOC/EMV

Source: Internet
Author: User

Reprinted please indicate the source

Author: Pony


Most of PBOC's IC card data is in the TLV format. EMV manual describes simple encoding rules.
I will analyze the TLV encoding format in detail and provide the corresponding pseudo code for TLV decoding.

 

TLV is the abbreviation of tag, length, and value. A basic data element includes the above three fields. The tag uniquely identifies the data element, and length is the length of the Value Field.
Value is the data itself. For example, the following is a TLV format of the aid (application identifier) byte string "9f0607a0000000031010", where 9f06 is a tag,
07 is the length, and a0000000031010 is the value of aid itself.

 

Developers should be concerned about how to extract the data we want if a TLV-encoded byte string is passed from the card?
This is the problem of TLV decoding.

 

The BER-TLV encoding is an iso-defined specification, which is then simplified to PBOC/EMV.
For example, the tag field can contain multiple bytes in ISO, while PBOC/EMV only requires the first two bytes. the TLV decoding I will talk about below is a simplified version based on PBOC/EMV.

 

First, let's take a look at how the tag field is encoded. The tag field occupies up to two bytes. the encoding rules are shown in the following two figures.

 

Figure 1
Figure 2

 

 

Explain the two images. the first figure shows the encoding rules of the first byte. type of B8 and B7 tags. this can be ignored for the moment. b6 determines that the current TLV data is a single data and composite data. the compound TLV indicates that the value field also contains one or more TLV, similar to the nested encoding format.
B5 ~ If the value of B1 is 1, there is a sub-byte under the tag, which occupies two bytes; otherwise, the tag occupies one byte.

The second figure shows the encoding format of the second byte if the tag occupies two bytes. b8 determines whether the tag contains any bytes of the Post-thread. As we have mentioned earlier, the tag in PBOC/EMV occupies up to two bytes, so this bit is kept as 0.

 

The above tag encoding format is clear, and the Code for decoding the tag domain can be easily written. Assume that the terminal receives the byte string, which is saved in the byte array of tlvdata. The pseudocode is as follows:

If (tlvdata [I] & 0x20 )! = 0x20) // single structure {If (tlvdata [I] & 0x1f) = 0x1f) // tag two bytes {tagindex ++; // parse the length field // parse the value field} else // tag a single byte {// parse the length field // parse the value field} else // Composite Structure {// Composite Structure we can consider using recursive methods .}

The length field is relatively simple to encode, with a maximum of four bytes. If the maximum B8 of the first byte is 0, B7 ~ The B1 value is the length of the value field. If B8 is 1,
B7 ~ The value of B1 indicates the following sub-bytes. the value of the lower face byte is the length of the Value Field. the encoding format of the value field depends on the data elements expressed by the specific value. for example, aid is composed of RID + pix. this is not detailed. with the above knowledge, you can basically write a TLV decoder.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.