用Java解析ASN.1二进制数据 [英] Parsing ASN.1 binary data with Java

查看:1137
本文介绍了用Java解析ASN.1二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要解析到我的Java项目中的二进制ASN.1数据对象。我只想要ASN.1结构和数据,例如BER查看器解析它:

I have binary ASN.1 data objects I need to parse into my Java project. I just want the ASN.1 structure and data as it is parsed for example by the BER viewer:

BouncyCastle的ASN.1解析器无法解析此结构(仅返回特定于应用程序的二进制数据类型)。

The ASN.1 parser of BouncyCastle is not able to parse this structure (only returns application specific binary data type).

我可以使用什么ASN.1库来获得这样的结果?有没有人有示例代码来演示如何解析ASN.1对象?

BTW:我还尝试了几个免费的ASN.1 Java编译器,但没有一个是能够生成ASN.1规范给出的工作Java代码。

BTW: I also tried several free ASN.1 Java compilers but none is able to generate working Java code given may ASN.1 specification.

推荐答案

我必须纠正自己 - 可以使用BouncyCastle中包含的ASN.1解析器读出数据 - 但是过程并不那么简单。

I have to correct myself - it is possible to read out the data using ASN.1 parser included in BouncyCastle - however the process is not that simple.

如果你只想打印ASN.1结构中包含的数据,我建议你使用类org.bouncycastle.asn1.util.ASN1Dump。它可以被以下简单的代码片段使用:

If you only want to print the data contained in an ASN.1 structure I recommend you to use the class org.bouncycastle.asn1.util.ASN1Dump. It can be used by the following simple code snippet:

ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(data));
ASN1Primitive obj = bIn.readObject();
System.out.println(ASN1Dump.dumpAsString(obj));

它打印结构但不打印数据 - 但是通过将ASN1Dump复制到自己的类并修改它要打印出例如OCTET_STRINGS,这可以很容易地完成。

It prints the structure but not the data - but by copying the ASN1Dump into an own class and modifying it to print out for example OCTET_STRINGS this can be done easily.

此外,ASN1Dump中的代码演示了解析ASN.1结构。对于该示例,我可以使用以下代码更深入地解析我的问题中使用的数据:

Additionally the code in ASN1Dump demonstrates to parse ASN.1 structures. For the example the data used in my question can be parsed one level deeper using the following code:

DERApplicationSpecific app = (DERApplicationSpecific) obj;
ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE);
Enumeration secEnum = seq.getObjects();
while (secEnum.hasMoreElements()) {
    ASN1Primitive seqObj = (ASN1Primitive) secEnum.nextElement();
    System.out.println(seqObj);
}

这篇关于用Java解析ASN.1二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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