IT,Change Myself;Change My Life;Change The World
阿新 • • 發佈:2018-12-31
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 |[email protected]...|
00000010 7b da 3c 76 ea 71 87 bb cd d8 db e6 35 02 40 13 |{.<[email protected]|
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 |........|
00000048
shell:~/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">�²öÎ÷u®”@ҁ{Ú<vêq‡»ÍØÛæ5@¹žú</P>
<P O="37" T="[UNIVERSAL 2]" TL="2" V="33" A="INTEGER">�®tR„UÕqFÛ("°X%Š¨c±Ò…øÌ™ç�߯³ä</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
1234567812345678
123456789123456789