bertool

Created: 2014-05-18 22:52
Updated: 2018-03-26 18:55
License: apache-2.0

README.md

BerTool

Build Status Coverage Status Maven Central

Basic Encoding Rules tool. BerTool was primarily created to decode EMV records encoded in tag-length-value or BER TLV format (TLV is also known as type-length value). With this tool it is easy not only to decode but to encode records too.

How to encode

 ├─[6F]
 │  ├─[84] 315041592E5359532E4444463031
 │  └─[A5]
 │     ├─[88] 02
 │     └─[5F2D] 656E
 └─[9F36] 0060

code

  BerBuilder builderA5 = BerBuilder.newInstance();
  builderA5.add(0x88, new byte[] {0x02});
  builderA5.addAsciiString(0x5F2D, "en");

  BerBuilder builder6F = BerBuilder.newInstance();
  builder6F.addHexString(0x84, "315041592E5359532E4444463031");
  builder6F.add(0xA5, builderA5);

  BerBuilder builder = BerBuilder.newInstance();
  builder.add(0x6F, builder6F);
  builder.add(0x9F36, new byte[] {0x00, 0x60});

  ByteBuffer buffer = ByteBuffer.allocate(builder.length());
  BerEncoder berEncoder = new BerEncoder();
  berEncoder.encode(builder, buffer);

How to decode

  byte[] byteArray = BerUtil.hexToByteArray("6F1A840E315041592E5359532E444446"
    + "3031A5088801025F2D02656E9f36020060");

  BerDecoder berDecoder = new BerDecoder();
  BerFrame berFrame = berDecoder.decode(byteArray);

  byte[] tag84 = berFrame.getContent(0x84)
  byte[] tag5F2D = berFrame.getContent(0x5F2D);

StreamBerPrinter example

with DefaultBerFormatter

  byte[] byteArray = BerUtil.hexToByteArray("6F1A840E315041592E5359532E444446"
    + "3031A5088801025F2D02656E77299f2701009f360200609f2608c2c12b098f3d"
    + "a6e39f10120111258013423a02cfec00000002011400ff9000"); 

  BerDecoder berDecoder = new BerDecoder();
  BerFrame berFrame = berDecoder.decode(byteArray);

  BerPrinter printer = new StreamBerPrinter(System.out);
  printer.print(berFrame);

output

 ├─[6F] 840E315041592E5359532E4444463031A5088801025F2D02656E
 │  ├─[84] 315041592E5359532E4444463031
 │  └─[A5] 8801025F2D02656E
 │     ├─[88] 02
 │     └─[5F2D] 656E
 ├─[77] 9F2701009F360200609F2608C2C12B098F3DA6E39F10120111258013423A02CFEC00000002011400FF
 │  ├─[9F27] 00
 │  ├─[9F36] 0060
 │  ├─[9F26] C2C12B098F3DA6E3
 │  └─[9F10] 0111258013423A02CFEC00000002011400FF
 └─[90]

with EmvBerFormatter

  byte[] byteArray = BerUtil.hexToByteArray("6F1A840E315041592E5359532E444446"
    + "3031A5088801025F2D02656E77299f2701009f360200609f2608c2c12b098f3d"
    + "a6e39f10120111258013423a02cfec00000002011400ff9000"); 

  BerDecoder berDecoder = new BerDecoder();
  BerFrame berFrame = berDecoder.decode(byteArray);

  BerPrinter printer = new StreamBerPrinter(System.out
      , EmvBerFormatter.newInstanceWithSpaces());
  printer.print(berFrame);

output

 ├─[6F] File Control Information (FCI) Template
 │  │ 84 0E 31 50 41 59 2E 53  59 53 2E 44 44 46 30 31
 │  │ A5 08 88 01 02 5F 2D 02  65 6E
 │  ├─[84] Dedicated File (DF) Name
 │  │   31 50 41 59 2E 53 59 53  2E 44 44 46 30 31
 │  └─[A5] File Control Information (FCI) Proprietary Template
 │     │ 88 01 02 5F 2D 02 65 6E
 │     ├─[88] Short File Identifier (SFI)
 │     │   02
 │     └─[5F2D] Language Preference
 │         65 6E
 ├─[77] Response Message Template Format 2
 │  │ 9F 27 01 00 9F 36 02 00  60 9F 26 08 C2 C1 2B 09
 │  │ 8F 3D A6 E3 9F 10 12 01  11 25 80 13 42 3A 02 CF
 │  │ EC 00 00 00 02 01 14 00  FF
 │  ├─[9F27] Cryptogram Information Data
 │  │   00
 │  ├─[9F36] Application Transaction Counter (ATC)
 │  │   00 60
 │  ├─[9F26] Application Cryptogram
 │  │   C2 C1 2B 09 8F 3D A6 E3
 │  └─[9F10] Issuer Application Data
 │      01 11 25 80 13 42 3A 02  CF EC 00 00 00 02 01 14
 │      00 FF
 └─[90] Issuer Public Key Certificate

Download

Download the latest jar via Maven:

<dependency>
  <groupId>org.nightcode</groupId>
  <artifactId>bertool</artifactId>
  <version>0.4</version>
</dependency>

Feedback is welcome. Please don't hesitate to open up a new github issue or simply drop me a line at dmitry@nightcode.org.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more