In order to thank you for all members' support of our website, we are holding new events - Getting Free Samples by Paying Freight only.
Want to get free samples? Please check this post for more details.

Is there a java card parser for BER-TLV ?

Communication

Moderator: UNKNwYSHSA

KevinAli
Posts: 23
Joined: Fri Aug 21, 2015 3:38 am
Points :96
Contact:

Is there a java card parser for BER-TLV ?

Post by KevinAli » Wed Dec 30, 2015 1:45 am

Please check the subject.
And the length of tags and values ​​are not known. Any pointers?

Crawford
Posts: 38
Joined: Thu Sep 17, 2015 11:50 pm
Points :232
Contact:

Re: Is there a java card parser for BER-TLV ?

Post by Crawford » Sun Jan 03, 2016 11:16 pm

KevinAli wrote:Please check the subject.
And the length of tags and values ​​are not known. Any pointers?


Hi,

The code below may be helpful to you.

Code: Select all

public static Map<String, String> parseTLV(String tlv) {
    if (tlv == null || tlv.length()%2!=0) {
        throw new RuntimeException("Invalid tlv, null or odd length");
    }
    HashMap<String, String> hashMap = new HashMap<String, String>();
    for (int i=0; i<tlv.length();) {
        try {
            String key = tlv.substring(i, i=i+2);

            if ((Integer.parseInt(key,16) & 0x1F) == 0x1F) {
                // extra byte for TAG field
                key += tlv.substring(i, i=i+2);
            }
            String len = tlv.substring(i, i=i+2);
            int length = Integer.parseInt(len,16);

            if (length > 127) {
                // more than 1 byte for lenth
                int bytesLength = length-128;
                len = tlv.substring(i, i=i+(bytesLength*2));
                length = Integer.parseInt(len,16);
            }
            length*=2;

            String value = tlv.substring(i, i=i+length);
            //System.out.println(key+" = "+value);
            hashMap.put(key, value);
        } catch (NumberFormatException e) {
            throw new RuntimeException("Error parsing number",e);
        } catch (IndexOutOfBoundsException e) {
            throw new RuntimeException("Error processing field",e);
        }
    }

    return hashMap;
}

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 1 guest

JavaCard OS : Disclaimer