对BER(基本编码规则)感到困惑 [英] Confused about BER (Basic Encoding Rules)

查看:257
本文介绍了对BER(基本编码规则)感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试研究和理解BER(基本编码规则)。



我一直在使用网站 http://asn1-playground.oss.com/ 尝试使用不同的ASN.1对象并使用BER对其进行编码。



但是,即使是最简单的编码也会使我感到困惑。



让我们采用一个简单的ASN.1模式:

 世界模式定义自动标签:: = 
开始
人类:: =序列{ b $ b名称UTF8String
}
END

所以基本上这只是一个 SEQUENCE 具有一个名为 name 的单个UTF8String类型的字段。



与该序列匹配的值的示例如下:

  { Bob} 

因此,使用 http://asn1-playground.oss.com/ ,我生成以下数据的BER编码:

 伙计们人::: 
{
name Bob
}

我希望这会产生一个序列对象,然后是一个字符串对象。



我要做什么得到的是:

  30 05 80 03 42 6F 62 

现在,我了解这种编码的一些。第一个八位字节 30 identifier ,它告诉我们 SEQUENCE 类型是第一个对象。 30 的二进制值为 00110000 ,这意味着我们有一个 0 中的$ c>, 1 (表示构造)的PC(原始/构造)位和标签 10000 的数量(十进制的16),表示 SEQUENCE



<到目前为止,一切都很好。下一个值是 SEQUENCE 的字节长度,即 05



好的,到目前为止还算不错。



但是然后...我 完全困惑 ,下一个八位位组 80 。这意味着什么???我本来希望值为00001100(对于标记号12,表示UTF8String。)



80 非常简单: 03 表示长度为3,而 42 6F 62 本身就是UTF8String值, 鲍勃

解决方案

80是上下文特定的标记0 。请注意,在模块的开头使用了自动标签。这表明所有SEQUENCE,SET和CHOICE类型的组件都将具有特定于上下文的标记,其标记从[0]开始,并为每个后续组件加1。这样,您不必担心创建消息时的标签冲突,尤其是在处理可选或具有默认值的组件时。如果您将 AUTOMATIC更改为 EXPLICIT(我不建议您这样做),则会在编码中看到您期望的[UNIVERSAL 12]。



请注意表示自动标签仅应用于SEQUENCE,SET或CHOICE组件上的标签。它不适用于顶层组件,这就是为什么您看到SEQUENCE的[UNIVERSAL 16]而不是在那里看到上下文特定标签的原因。


I'm trying to study and understand BER (Basic Encoding Rules).

I've been using the website http://asn1-playground.oss.com/ to experiment with different ASN.1 objects and encoding them using BER.

However, even the simplest encodings seem to confuse me.

Let's take a simple ASN.1 schema:

World-Schema DEFINITIONS AUTOMATIC TAGS ::= 
BEGIN
  Human ::= SEQUENCE {
     name UTF8String
  }
END

So basically this is just a SEQUENCE with a single UTF8String type field called name.

An example of a value that matches this sequence would be something like:

{ "Bob" }

So, using http://asn1-playground.oss.com/, I produce the BER encoding of the following data:

some-guy Human ::= 
{  
    name "Bob"
}

I would expect this to produce one sequence object, followed by a single string object.

What I get is:

30 05 80 03 42 6F 62

Now, I understand some of this encoding. The first octet, 30, is the identifier which tells us that a SEQUENCE type is the first object. The 30 is 00110000 in binary, which means that we have a class of 0, a PC (primitive/constructed) bit of 1 (meaning constructed), and a tag number of 10000 (16 in decimal) which means SEQUENCE

So far so good. The next value is the LENGTH in bytes of the SEQUENCE, which is 05.

Okay, still so far so good.

But then... I'm totally confused by the next octet 80. What does that mean??? I would have expected a value of 00001100 (for tag number 12, meaning UTF8String.)

The bytes following the 80 are pretty straightforward: the 03 means Length of 3, and the 42 6F 62 is just the UTF8String value itself, "Bob"

解决方案

The 80 is a context-specific tag 0. Please note that "AUTOMATIC TAGS" is used at the beginning of the module. This indicates that all SEQUENCE, SET and CHOICE types will have context specific tags for their components starting with [0], and incrementing by 1 for each subsequent component. This way, you don't have to worry about tag conflicts when creating your messages, especially when dealing with components which are OPTIONAL or have a DEFAULT value. If you change "AUTOMATIC" to "EXPLICIT" (which I would not recommend) you will see the [UNIVERSAL 12] that you were expecting in the encoding.

Please note that AUTOMATIC TAGS applied only to tags on components of SEQUENCE, SET, or CHOICE. It does not apply to the top level components, which is why you saw the [UNIVERSAL 16] for the SEQUENCE rather than seeing a context-specific tag there also.

这篇关于对BER(基本编码规则)感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆