使用充气城堡解析ASN文件 [英] parsing ASN files using bouncy castle

查看:96
本文介绍了使用充气城堡解析ASN文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 JASN1



我已经使用语法文件
成功生成了Java类,但是我没有需要解码的CDR,但是我无法使其正常工作,我不知道它需要什么样的输入



我已经到了可以将CDR解析为以下行的位置

  [1] [[0]#01,[1]#26fd,[3]#4131002400,[8]#14040020236233,[9]#21436500000041,[10]#196105000045ffffffffffffff,[13]#13900049999957,[14 ]#21436549999961,[15]#05,[16]#05,[17]#116102999954ffffffffffff,[22]#00a2,[23]#0001,[37]#0010,[38]#03,[40]# 0324,[46]#06,[47]#05,[54]#00580720111220,[85]#04f4] b 
$ b

Java代码

 公共类JASN1 {

public static void main(String [ ] args)throws IOException {
// TODO自动生成的方法存根

ByteArrayInput流bais = new ByteArrayInputStream(readContentIntoByteArray(new File( sample.asn))));

ASN1InputStream ais = new ASN1InputStream(new FileInputStream(new File( sample.asn))));

而(ais.available()> 0){
DERTaggedObject基元=(DERTaggedObject)ais.readObject();

System.out.println(primitive.toASN1Object());

编码的字符串= toHexadecimal(new String(primitive.getEncoded()));

bais = new ByteArrayInputStream(encoded.getBytes());

MobileSampleMsg mobileSampleMsg = new MobileSampleMsg();

mobileSampleMsg.decode(bais,true);

System.out.println( MobileSampleMsg = + mobileSampleMsg);

}
ais.close();

/ *
* System.out.println(bais); MobileSampleMsg personRecord_decoded =
*新的MobileSampleMsg(); personRecord_decoded.decode(bais,true);
*
* System.out.println();
* System.out.println( PersonnelRecord.name.givenName = +
* personRecord_decoded);
* /

}

私有静态字节[] readContentIntoByteArray(File file){
FileInputStream fileInputStream = null;
byte [] bFile =新的byte [(int)file.length()];
try {
//将文件转换为字节数组
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
} catch(Exception e){
e.printStackTrace();
}
返回bFile;
}

公共静态字符串toHexadecimal(String text)抛出UnsupportedEncodingException {
byte [] myBytes = text.getBytes( UTF-8);

返回DatatypeConverter.printHexBinary(myBytes);
}
}

此处
下载语法

解决方案

我能够在JASN1和BouncyCastle的帮助下对文件进行编码/解码,我使用了JASN1将语法文件编译为Java类,然后使用BouncyCastle解码/编码新对象,下面是用于显示代码的代码段

 公共类BouncyCastle {

public static void main(String [] args)抛出IOException {

DetailOutputRecord detailOutputRecord = new DetailOutputRecord();
MyRecord myRecord = new MyRecord();

myRecord.setOriginNodeType(new NodeType( xxxx .getBytes()));
myRecord.setTransactionAmount(new MoneyAmount( xxxx .getBytes()));
myRecord.setSubscriberNumber(new NumberString( xxxx .getBytes()));

ReverseByteArrayOutputStream ros = new ReverseByteArrayOutputStream(1000);

detailOutputRecord.setMyRecord(myRecord);
myRecord.encode(ros);
System.out.println(DatatypeConverter.printHexBinary(ros.getArray()));
System.out.println(print(ros.getArray()));

DERTaggedObject dermyRecord = new DERTaggedObject(false,6,ASN1Primitive.fromByteArray(ros.getArray()));


文件f =新文件(String.valueOf( 1_dermyRecord.ASN));
FileOutputStream流=新的FileOutputStream(f);
try {
stream.write(dermyRecord.getEncoded());
}最终{
stream.close();
}

ros = new ReverseByteArrayOutputStream(1000);
detailOutputRecord.encode(ros);

DLSequence ddetailOutputRecord = new DLSequence(ASN1Primitive.fromByteArray(ros.getArray()));
stream = new FileOutputStream(new File( detailOutputRecord.ASN));
try {
stream.write(ros.buffer);
}最终{
stream.close();
}



}

public static String print(byte [] bytes){
StringBuilder sb = new StringBuilder ();
sb.append( [);
for(字节b:字节){
// sb.append(String.format( 0x%02X,b));
sb.append(String.format( \\x%02X,b));
}
sb.append(]);
return sb.toString();
}

私有静态DERTaggedObject toDERObject(byte [] data)引发IOException {
ByteArrayInputStream inStream = new ByteArrayInputStream(data);
ASN1InputStream asnInputStream =新的ASN1InputStream(inStream);

return(DERTaggedObject)asnInputStream.readObject();
}
}


am trying to parse binary CDRs using JASN1

I have successfully generated Java classes using grammer file not I have a CDR which I need to decode, but I can't get it to work, I don't understand what kind of inputs it requires

I have reached a point where I can parse CDR into lines like below

[1][[0]#01, [1]#26fd, [3]#4131002400, [8]#14040020236233, [9]#21436500000041, [10]#196105000045ffffffffffff, [13]#13900049999957, [14]#21436549999961, [15]#05, [16]#05, [17]#116102999954ffffffffffff, [22]#00a2, [23]#0001, [37]#0010, [38]#03, [40]#0324, [46]#06, [47]#05, [54]#00580720111220, [85]#04f4]

Java code

public class JASN1 {

        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub

            ByteArrayInputStream bais = new ByteArrayInputStream(readContentIntoByteArray(new File("sample.asn")));

            ASN1InputStream ais = new ASN1InputStream(new FileInputStream(new File("sample.asn")));

            while (ais.available() > 0) {
                DERTaggedObject primitive = (DERTaggedObject) ais.readObject();

                 System.out.println(primitive.toASN1Object());

                String encoded = toHexadecimal(new String(primitive.getEncoded()));

                bais = new ByteArrayInputStream(encoded.getBytes());

                MobileSampleMsg mobileSampleMsg = new MobileSampleMsg();

                mobileSampleMsg.decode(bais, true);

                System.out.println("MobileSampleMsg = " + mobileSampleMsg);

            }
            ais.close();

            /*
             * System.out.println(bais); MobileSampleMsg personnelRecord_decoded =
             * new MobileSampleMsg(); personnelRecord_decoded.decode(bais, true);
             * 
             * System.out.println("");
             * System.out.println("PersonnelRecord.name.givenName = " +
             * personnelRecord_decoded);
             */

        }

        private static byte[] readContentIntoByteArray(File file) {
            FileInputStream fileInputStream = null;
            byte[] bFile = new byte[(int) file.length()];
            try {
                // convert file into array of bytes
                fileInputStream = new FileInputStream(file);
                fileInputStream.read(bFile);
                fileInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bFile;
        }

        public static String toHexadecimal(String text) throws UnsupportedEncodingException {
            byte[] myBytes = text.getBytes("UTF-8");

            return DatatypeConverter.printHexBinary(myBytes);
        }
    }

download samples from here download grammer from here

解决方案

I was able to encode/decode the file with the help of JASN1 and BouncyCastle, I used JASN1 to compile grammer file into java classes then used BouncyCastle to decode/encode new objects, below is code snippet used to show how I did that

public class BouncyCastle {

    public static void main(String[] args) throws IOException {

        DetailOutputRecord detailOutputRecord = new DetailOutputRecord();
        MyRecord myRecord = new MyRecord();

        myRecord.setOriginNodeType(new NodeType("xxxx".getBytes()));
        myRecord.setTransactionAmount(new MoneyAmount("xxxx".getBytes()));
        myRecord.setSubscriberNumber(new NumberString("xxxx".getBytes()));

        ReverseByteArrayOutputStream ros = new ReverseByteArrayOutputStream(1000);

        detailOutputRecord.setMyRecord(myRecord);
        myRecord.encode(ros);
        System.out.println(DatatypeConverter.printHexBinary(ros.getArray()));
        System.out.println(print(ros.getArray()));

        DERTaggedObject dermyRecord = new DERTaggedObject(false, 6, ASN1Primitive.fromByteArray(ros.getArray()));


        File f = new File(String.valueOf("1_dermyRecord.ASN"));
        FileOutputStream stream = new FileOutputStream(f);
        try {
            stream.write(dermyRecord.getEncoded());
        } finally {
            stream.close();
        }

        ros = new ReverseByteArrayOutputStream(1000);
        detailOutputRecord.encode(ros);

        DLSequence ddetailOutputRecord = new DLSequence(ASN1Primitive.fromByteArray(ros.getArray()));
        stream = new FileOutputStream(new File("detailOutputRecord.ASN"));
        try {
            stream.write(ros.buffer);
        } finally {
            stream.close();
        }



    }

    public static String print(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        sb.append("[ ");
        for (byte b : bytes) {
            // sb.append(String.format("0x%02X ", b));
            sb.append(String.format("\\x%02X", b));
        }
        sb.append("]");
        return sb.toString();
    }

    private static DERTaggedObject toDERObject(byte[] data) throws IOException {
        ByteArrayInputStream inStream = new ByteArrayInputStream(data);
        ASN1InputStream asnInputStream = new ASN1InputStream(inStream);

        return (DERTaggedObject) asnInputStream.readObject();
    }
}

这篇关于使用充气城堡解析ASN文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆