ASN1 研究4

类型规则

                  

For small value tags, from 0 to 30, the actual value of the tag is stored in the bottom five bits, where the value associated with a normal ASN.1 tag goes otherwise. For tag values from 31 to 127, the bottom five bits are all set to 1 and the next octet is used to contain the tag number. If the number is higher than 127, the top bit of the next octet is set to 1 and the number is stored in seven-bit chunks, with the top bit of each octet being set to 1 if there is another chunk to follow.

By way of example, if you ignore whether bit 6 gets set for the moment, an ASN.1 object that has been given a 0 tag, as in has[0] in front of its type, will be the byte 0×80. If it has been tagged with[32], the tag will take two bytes—0×9f to indicate a tag greater than 31, and 0×20 to give the actual value of the tag. Finally, if it has been tagged with “[128]’ there will be three bytes of tag—0×9f, 0×81 to give the first seven bits of the value with the bit 8 set to indicate another byte follows, and 0x00 to give the next seven bits of the value with bit 8 not set to indicate it is the last seven bits of the tag value.

对于小的tag值,从0到30,实际 的tag值是存储在低5位,对于从31到127的tag,低五位都是设置为1,下一个octet用来包含实际的tag值,如果大于127的tag值,那么接下来的octet最高位设置为1,值是存储在7位中,如果还有数据块,那么接下来的每个octet高位都设置为1

另外,如果忽略了bit6位,一个asn1对象有一个值为0的tag,就是[0]在其类型之前,那么tag值将会是0x80.

如果有一个[32]的tag,那么tag将会用两个字节表示,第一个0x9f(低5位都是1)代表这是一个大于31的,第二个0x20代表了实际的tag值。

最后,如果有一个[128]的tag,那么将会有3个字节的tag,第一个0x9f,第二个0x81(高位设置为1,说明后面还有octed),第三个:0x00(高位没有设置,接下来的7位块都为0)

如果是[200]的tag 那么会是:0x 9f 0x81 0x48

编码规则:

    1、原始定长编码:

                                        要求知道将要编码的长度,适合非构造类型,或者隐式的Tagg类型,对这种类型编码,首先需要编码Tagg对象的值,然后编码长度,最后编码内容。

                                       编码长度是值得分析的,如果长度小于或者等于127,那么将写一个单独的Octet,用7位数包含实际的长度。如果是大于127,第一个Octet将是bit8位设置为1,bit7到bit1位代表接下来的octet的个数,然后写出编码的长度,高位在前。

例如:127将会是编码:0x7f              128将会是编码0x81 0x80   1000将会是编码0x82 0x03 0xe8

The definite-length methods all require that you know the length of what you are trying to encode in advance. The primitive definite-length method is appropriate for any nonstructured type, or implicitly tagged versions of the same, and an encoding of this type is created by first encoding the tag assigned to the object, encoding the length, and then writing out the encoding of the body.

You'll look at how the bodies are encoded in more detail later, but how the encoding of the length is done is worth looking at here. If the length is less than or equal to 127, a single octet is written out containing the actual length as a 7-bit number. If the length is greater than 127, the first octet written out has bit 8 set and bits 7-1, represent the number of octets following that contain the actual length. The length is then written out, one octet at a time, high order octet first.

For example, a length of 127 will produce a length encoding with 1 byte of the value 0×7f, a length of 128 will produce a 2-byte encoding with the values 0×81 and 0×80, and a length of 1,000 will produce a 3 byte encoding—0×82, 0×03, and 0×e8. This is the simplest method of encoding and, as you will see, is required for DER encodings.

 

 

ans1定义了3种Octet-based编码规则

(1)ber

(2)der

(3)cer

这三种都是TLV-based,per是bit-based编码

 

TLV代表了: T:type或者tag      L:长度(确定或者不确定)         V:值(L个八位元组,如果L是确定的)

Optional “End-Of-Content” octets if indefinite-length
V = TLV when EXPLICIT tagging or constructed form

如果是不确定长度的,那么有一个额外的“end-of-content”八位元组。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值