简体   繁体   中英

Java - Parsing Hex String to ASN.1 Format

I'm attempt to decoding my hex string to ASN.1 structure (tag - lenght - value) like this page: https://asn1.io/asn1playground/ But i got stuck with Bouncy Castle library. Is there any mothods resolving my issue?

My hex string values = "100101110D48434D432D46492D30312D3031120131130130010A464930303030303030331D0614050D0A3A04"

I need the decoding result like:

tag 1: 10 - lenght 1: 01 - value 1: 01
tag 2: 11 - lenght 2: 0D - value 2: 48434D432D46492D30312D3031
tag 1: 12 - lenght 1: 01 - value 1: 31
...

You can use the source code of my runtime lib (and modify it to achieve the exact result you want)

As it is now it works like that:

1/ Download asn1-runtime.jar from https://github.com/yafred/asn1-tool/releases

2/ Compile following class with asn1-runtime.jar in the classpath

package asn1;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import com.yafred.asn1.runtime.BERDumper;

public class Dump {

    public static void main(String[] args) throws IOException {
        String values = "100101110D48434D432D46492D30312D3031120131130130010A464930303030303030331D0614050D0A3A04";


        // dump TLV form
        ByteArrayInputStream bufferIn = new ByteArrayInputStream(BERDumper.bytesFromString(values));
        new BERDumper(new PrintWriter(System.out)).dump(bufferIn);

    }

}

3/ run it (with asn1-runtime.jar)

T: 10 (PRIMITIVE_UNIVERSAL_16)
L: 1
V: 01
T: 11 (PRIMITIVE_UNIVERSAL_17)
L: 13
V: 48 43 4d 43 2d 46 49 2d 30 31 2d 30 31
T: 12 (PRIMITIVE_UNIVERSAL_18)
L: 1
V: 31
T: 13 (PRIMITIVE_UNIVERSAL_19)
L: 1
V: 30
T: 01 (PRIMITIVE_UNIVERSAL_1)
L: 10
V: 46 49 30 30 30 30 30 30 30 33
T: 1d (PRIMITIVE_UNIVERSAL_29)
L: 6
V: 14 05 0d 0a 3a 04

It may be a good start if you don't find anything else...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM