将 ASN.1 中 OID 的第一个标识符编码为 BER

Encoding of first identifier of an OID in ASN.1 into BER

OID 似乎是一种表示树状结构的方法。

例如 1.3.6.1 是 iso(1).org(3).did(6).internet(1)

最常用的编码方案似乎是BER,我对前两位数字的编码感到困惑。为什么前两位编码成一个字节,为什么是这样:

int first_digit = first_byte / 40;
int second_byte = first_byte % 40;

是否假设前两个字节总是小数,因此可以很容易地'stored'一个字节?如果是这样我就明白了。

但为什么要使用神奇的数字 40?为什么是 40?

例如。上面 1.3.xxx 案例中的第一个字节将被编码为 43.

引自“ASN.1 Complete”一书,教授。 John Larmouth,第 3.14 节(该书可从 http://www.oss.com/asn1/resources/books-whitepapers-pubs/larmouth-asn1-book.pdf 免费获得):

The octets encoding the first two arcs were (in 1986) thought to be unlikely to ever have large values, and that using two octets for these two arcs was "a bad thing". So an "optimization" (mandatory) was introduced.

Well, there are three top-level arcs, and we can accommodate encodings for up to 128 arcs (0 to 127) in a single octet with the "more bit" concept described above. 128 divided by 3 is about 40! Let's assume the first two top-level arcs will never have more than 40 sub-arcs, and allocate the first 40 pseudo-arcs to top-level arc 0, the next 40 to top-level arc 1, and the remainder to top-level arc 2.