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">�²öÎ÷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 1234567812345678123456789123456789
阅读全文
0 0
- JAVA中使用ASN.1
- openssl asn.1使用
- asn.1工具的使用
- 利用java开发ASN.1
- ASN.1 Java编译器示例
- asn.net 中使用ajax问题
- openssl中ASN.1源码解读
- ASN.1
- ASN.1
- ASN.1
- asn.1
- ASN.1
- ASN.1中OID类型的DER编码(附Perl,java源码)
- 如何看懂描述SNMP中MIB的ASN.1(转)
- 如何看懂描述SNMP中MIB的ASN.1
- 如何看懂描述SNMP中MIB的ASN.1
- 对asn.1抽象语法标记中Tag的理解
- 如何看懂描述SNMP中MIB的ASN.1 .
- CodeForces 25D Roads not only in Berland
- 王爽.汇编.第三版.检测点17.1.疑似答案
- 孤单数
- Retrofit简单封装
- python学习日志--day6
- JAVA中使用ASN.1
- Ubuntu下的一些命令整理+Windoms下MySql命令+javaWeb服务器搭建+Ubuntu下JDK安装
- 【Centos7笔记二】常用命令
- 2Linux下的socket介绍
- C++11新特性--线程库相关
- 小甲鱼课程学习016-027
- 移动端300ms的点击延迟以及解决方案
- 一只菜鸟的成长之路
- js小知识