2012-07-13 25 views
6

Soy nuevo en Java, por lo que me gustaría utilizar la solución estándar para, creo, la tarea estándar. La longitud de las etiquetas y los valores no se conocen.¿Hay un analizador de Java para BER-TLV?

+0

¿podría ser éste el que necesita? https://github.com/VakhoQ/tlv-encoder – grep

Respuesta

0

Encontré Javacard clases para BER TLV. Espero que los ayuda

3

Tutorial en here da un consejos sobre cómo analizar BER-TLV. Usando JACCAL

+0

esta es fantastci! – null

1

hice un analizador simple basado en la información proporcionada aquí: http://www.codeproject.com/Articles/669147/Simple-TLV-Parser

no sé si este apoyo código de toda la norma, pero funciona para mí.

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; 
} 
+0

simple-tlv es diferente de ber-tlv, y un analizador de tlv simple a menudo chocará en los datos ber-tvl –

0

Puede utilizar este analizador BER-TLV: source code on git o download jar.
Ejemplos:

cómo analizar

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

Cómo construir

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

podría ser this free library puede ser útil para usted. Lo he usado para el análisis simple de TLV. De todos modos, es con licencia de MIT y puedes modificarlo.

https://github.com/VakhoQ/tlv-encoder 
Cuestiones relacionadas