Java 如何使用 bouncycastle 解析 ASN1 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20373958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse the ASN1 data by using bouncycastle
提问by Arindam Mukherjee
I have the byte array which is in the form of asn1 format..now i want to parse this data to get required information..Here is the code they have written for c++..now i want to do the same in java.could any one help on this.
我有 asn1 格式的字节数组..现在我想解析这个数据以获得所需的信息..这是他们为 c++ 编写的代码..现在我想在 java.could 中做同样的事情任何人对此都有帮助。
ASN1 asn1(in);
ASN1 asn1(in);
int startPos = in.tellg();
asn1.enterObject(asn1.getTag());
int endPos = asn1.getCurrentLength() + in.tellg();
ubytearray acctId, valData;
while (in.tellg() < endPos) {
asn1.enterObject(0x30); //0x30 TAG_SEQ
// read the name
ubytearray nameData = asn1.getContent(ASN1_TAG_UTF8_STRING);
ubytearray octstr = asn1.getContent(ASN1_TAG_OCTET_STRING);
ASN1 asn2(octstr); asn2.enterObject(0x30); ustring urlstr(asn2.getContent(ASN1_TAG_UTF8_STRING)); ustring nameStr(asn2.getContent(ASN1_TAG_UTF8_STRING)); asn2.enterObject(0x30); ubytearray certs = asn2.getContent(ASN1_TAG_OCTET_STRING);
ASN1 asn2(octstr); asn2.enterObject(0x30); ustring urlstr(asn2.getContent(ASN1_TAG_UTF8_STRING)); ustring nameStr(asn2.getContent(ASN1_TAG_UTF8_STRING)); asn2.enterObject(0x30); ubytearray certs = asn2.getContent(ASN1_TAG_OCTET_STRING);
if ((urlstr.length() > 0) && (nameStr.length() > 0) && (certs.length() > 0)) {
printf("url %s\n", urlstr.c_str());
printf("name %s\n", nameStr.c_str());
printf("certs len:%d\n", certs.length());
} }
} }
回答by Aamirkhan
You can decode byteArray like this
你可以像这样解码 byteArray
String base64String = "Here is your byte array data";
byte[] data = Base64.decode(base64String, Base64.DEFAULT);
String result= new String(data , "UTF-8");
回答by Peter Dettman
You should be able to use the classes in org.bouncycastle.asn1, beginning with ASN1InputStream, to read the ASN.1 structure from an InputStream or byte[]. It's a little bit difficult to follow your example code, but something like this might help you get started:
您应该能够使用 org.bouncycastle.asn1 中的类,从 ASN1InputStream 开始,从 InputStream 或 byte[] 读取 ASN.1 结构。遵循您的示例代码有点困难,但这样的事情可能会帮助您入门:
byte[] data = null; // obviously need to supply real data here
ASN1InputStream input = new ASN1InputStream(data);
ASN1Primitive p;
while ((p = input.readObject()) != null) {
ASN1Sequence asn1 = ASN1Sequence.getInstance(p);
DERUTF8String nameData = DERUTF8String.getInstance(asn1.getObjectAt(0));
ASN1OctetString octstr = ASN1OctetString.getInstance(asn1.getObjectAt(1));
ASN1Sequence asn2 = ASN1Sequence.getInstance(ASN1Primitive.fromByteArray(octstr.getOctets()));
// ... and so on
}
If you find the structure of the data hard to understand, perhaps the org.bouncycastle.asn1.util.ASN1Dump class will be helpful:
如果您发现数据的结构难以理解,也许 org.bouncycastle.asn1.util.ASN1Dump 类会有所帮助:
byte[] data = null; // obviously need to supply real data here
ASN1InputStream input = new ASN1InputStream(data);
ASN1Primitive p;
while ((p = input.readObject()) != null) {
System.out.println(ASN1Dump.dumpAsString(p));
}