Is there a Java parser for BER-TLV?

I am new to Java, so I would like to use a standard solution for a standard task. The length of tags and values ​​is unknown.

+6
source share
6 answers

The tutorial in here gives tips on how to analyze BER-TLV. Using jaccal

+3
source

I made a simple parser based on the information provided here: http://www.codeproject.com/Articles/669147/Simple-TLV-Parser

I don't know if this code supports the entire standard, but it works for me.

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; } 
+1
source
+1
source

I found Javacard classes for BER TLV. Hope this helps.

0
source

You can use this BER-TLV parser: git source code or download jar .
Examples:

How to disassemble

 byte[] bytes = HexUtil.parseHex("50045649534157131000023100000033D44122011003400000481F"); BerTlvParser parser = new BerTlvParser(LOG); BerTlvs tlvs = parser.parse(bytes, 0, bytes.length); 

How to create

 byte[] bytes = new BerTlvBuilder() .addHex(new BerTag(0x50), "56495341") .addHex(new BerTag(0x57), "1000023100000033D44122011003400000481F") .buildArray(); 
0
source

Maybe this free library may be useful to you. I used this method for simple TLV parsing. In any case, it is licensed with MIT, and you can change it.

 https://github.com/VakhoQ/tlv-encoder 
0
source

Source: https://habr.com/ru/post/920432/


All Articles