简体   繁体   中英

How to decode asn.1 data in java

I have a asn.1 file whose content is unknown as I am unable to read it properly. I found some answer in stackoverflow but i have some doubt regarding this.

public static void main(String[] args) throws IOException {
    ASN1InputStream ais = new ASN1InputStream( new FileInputStream(new File("asnfile")));
    while (ais.available() > 0){
        DERObject obj = ais.readObject();
        System.out.println(ASN1Dump.dumpAsString(obj, true));
        //System.out.println(CustomTreeNode.dumpAsString(obj));
     }
     ais.close();
}

The output of the code looks like below:

> 00                                                                  
>             Tagged [0] IMPLICIT 
>                 DER Sequence
>                     Tagged [0] IMPLICIT 
>                         DER Octet String[1] 
>                             00                                                                  
>                     Tagged [1] IMPLICIT 
>                         DER Octet String[8] 
>                             15051312215238f6                                                    !R8
>                     Tagged [2] IMPLICIT 
>                         DER Octet String[8] 
>                             53968510617268f0                                                    Sarh
>                     Tagged [3] IMPLICIT

I think it is not the actual format. How can I read the file and what are the jars required to add in my project to read the file.

You appear to be using the Bouncy Castle ASN1 reading class. That is a very well-regarded library; it's quite unlikely that it produces the wrong answer. So, it seems, the answer to your question is that you are already using the right tool. Why don't you find a tool that just dumps ASN.1 files outside of Java and compare your results to that.

You got a string dump of encoded stream and just use the following cod snippet (i have not altered as per your answer just understand the following code then you can do it)

While writing this code i use to read a block of 2048 stream every time

ASN1InputStream bIn = null;
Object decodedObj = null; 

while ((len = inputStream.read(buf, 0, size)) != -1) { // Read 2Kb Block 

                bIn = new ASN1InputStream(buf); // Passing buffer

                try{

                    while( (decodedObj = bIn.readObject()) != null){ // Read each record in the Block.

                            decode(decodedObj);

                    }
                }//try block 
                catch (Exception e){
                    //e.printStackTrace();
                    //logger.error("Got an error while dividing inputStream because of filler bytes :"+e.getMessage());
                }
            }

decode method content as fallow

void decode(Object obj){

    ASN1Primitive primObj = null;

    if(obj instanceof ASN1TaggedObject)
           {
               mapDecodedMsgValues = new HashMap<String,String>();
               msgTag = ((ASN1TaggedObject)obj).getTagNo() ;

    switch (msgTag)
               {
                    case ABC_EVENT: // you can keep required tag number 

                        primObj = ((DERTaggedObject)obj).getObject();

                        if (primObj instanceof ASN1Sequence)
                        {

                           ASN1Sequence obj = (ASN1Sequence)primObj;
                           ASN1Encodable imsi_asn1Enc  = obj.getObjectAt(IMSI_INDEX); // write which index value you want
                           ASN1Primitive imsi_asn1Prim = imsi_asn1Enc.toASN1Primitive();
                           ASN1OctetString imsi_OS = (ASN1OctetString) ((ASN1TaggedObject)imsi_asn1Prim).getObject(); //According to spec is OCTETString
                           raw_imsi = imsi_OS.toString();
                        }
                 }
}

where raw_imsi variable will give decoded specific index data

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