JAVA中使用ASN.1

来源:互联网 发布:python 爬虫 视频教程 编辑:程序博客网 时间:2024/06/06 17:56

ASN.1是什么?

具体是什么去自己搜索吧

ASN.1是一种编码格式,只要遵循固定格式标准,都可以解析ASN编码。先举个简单例子,联系信息数据
假设01代表名字,02代表电话下面分别分别张三和王二麻子的 电话号码分别为12345678和123456.

01 02 张 三 02 08 01 02 03 04 05 06 07 08
01 04 王 二 麻 子 02 06 01 02 03 04 05 06

每一项的格式都是固定:

tag + 长度+具体内容

java类sun.security.util.DerValue中一下标签

 public final static byte    tag_Boolean = 0x01; public final static byte    tag_Integer = 0x02; public final static byte    tag_BitString = 0x03; public final static byte    tag_OctetString = 0x04; public final static byte    tag_Null = 0x05; public final static byte    tag_ObjectId = 0x06; public final static byte    tag_Enumerated = 0x0A; public final static byte    tag_UTF8String = 0x0C; public final static byte    tag_PrintableString = 0x13; public final static byte    tag_T61String = 0x14; public final static byte    tag_IA5String = 0x16; public final static byte    tag_UtcTime = 0x17; public final static byte    tag_GeneralizedTime = 0x18; public final static byte    tag_GeneralString = 0x1B; public final static byte    tag_UniversalString = 0x1C; public final static byte    tag_BMPString = 0x1E; public final static byte    tag_Sequence = 0x30; public final static byte    tag_SequenceOf = 0x30; public final static byte    tag_Set = 0x31; public final static byte    tag_SetOf = 0x31;

ASN查看工具

ASN格式的文件可以结合hexdump和unber两个工具进行查看。

shell:~/work$ hexdump -C new_sign.pem     00000000  30 46 02 21 00 b2 f6 ce  f7 75 ae 94 40 0b d2 81  |0F.!.....u..@...|    00000010  7b da 3c 76 ea 71 87 bb  cd d8 db e6 35 02 40 13  |{.<v.q......5.@.|    00000020  14 b9 03 9e fa 02 21 00  ae 74 9d 52 84 55 d5 71  |......!..t.R.U.q|    00000030  46 db 28 22 b0 58 25 8a  a8 63 b1 d2 85 f8 cc 16  |F.(".X%..c......|    00000040  99 e7 00 df af b3 03 e4                           |........|    00000048shell:~/work$ unber new_sign.pem         <C O="0" T="[UNIVERSAL 16]" TL="2" V="70" A="SEQUENCE">            <P O="2" T="[UNIVERSAL 2]" TL="2" V="33" A="INTEGER">&#x00;&#xb2;&#xf6;&#xce;&#xf7;&#x75;&#xae;&#x94;&#x40;&#x0b;&#xd2;&#x81;&#x7b;&#xda;&#x3c;&#x76;&#xea;&#x71;&#x87;&#xbb;&#xcd;&#xd8;&#xdb;&#xe6;&#x35;&#x02;&#x40;&#x13;&#x14;&#xb9;&#x03;&#x9e;&#xfa;</P>            <P O="37" T="[UNIVERSAL 2]" TL="2" V="33" A="INTEGER">&#x00;&#xae;&#x74;&#x9d;&#x52;&#x84;&#x55;&#xd5;&#x71;&#x46;&#xdb;&#x28;&#x22;&#xb0;&#x58;&#x25;&#x8a;&#xa8;&#x63;&#xb1;&#xd2;&#x85;&#xf8;&#xcc;&#x16;&#x99;&#xe7;&#x00;&#xdf;&#xaf;&#xb3;&#x03;&#xe4;</P>        </C O="72" T="[UNIVERSAL 16]" A="SEQUENCE" L="72">

上面文件文件开始是个0x30,也就是代表SEQUENCE,长度位0x46(70),接下来是个tag 0x02和ox21,代表接下来是一个铲毒位33的整数,接下来也是如此。

应用

java的sun.security.util包中提供的DerInputStrem和DerOutputStream可以用于读写ASN格式。

        BigInteger int1=new BigInteger("1234567812345678");        BigInteger int2=new BigInteger("123456789123456789");        DerOutputStream dout=new DerOutputStream();        dout.putInteger(int1);        dout.putInteger(int2);        DerOutputStream sequence=new DerOutputStream();        sequence.write(DerValue.tag_Sequence, dout);        HexDumpEncoder encoder=new HexDumpEncoder();        byte[] result=sequence.toByteArray();        System.out.println(encoder.encode(result));        DerInputStream din=new DerInputStream(result);        DerValue[] value=din.getSequence(0);        System.out.println(value[0].getBigInteger());        DerInputStream din2=new DerInputStream(value[1].toByteArray());        System.out.println(din2.getBigInteger());

输入结果为:

0000: 30 13 02 07 04 62 D5 37   E7 EF 4E 02 08 01 B6 9B  0....b.7..N.....0010: 4B AC D0 5F 15 1234567812345678123456789123456789
原创粉丝点击