【问题标题】:How do I encode using ASN.1?如何使用 ASN.1 进行编码?
【发布时间】:2016-02-04 18:42:48
【问题描述】:

我对 ANS1 编码非常陌生,我需要一些指导。

...假设我有以下 ANS1

MyType ::= SEQUENCE 
{ 
--lets assume hex value here is "01-01" for myText1
myText1 [0] IMPLICIT OCTET STRING OPTIONAL, 
myCheck [1] IMPLICIT INTEGER {doNow(0), doLater(1)} DEFAULT doNow,
myText2 [2] OCTET STRING
}

如果myText2的值为“12-34”,那么下面的十六进制字符串30-08-01-01-01-01-04-02-12-34是否会被认为是正确编码的数据?

...如果可选的 myText1 不存在,则编码为

30-06-01-01-04-02-12-34

30-07-00-01-01-04-02-12-34?

【问题讨论】:

    标签: asn.1


    【解决方案1】:

    假设我们要使用 BER 编码规则对值 {myText1 '0101'H, myCheck 0, myText2 '1234'H} 进行编码:

    30 0D                 SEQUENCE (universal tag 16, constructed) of length 13
       80 02 01 01        context specific implicit tag 0, length 2, value 0101
       81 01 00           context specific implicit tag 1, length 1, value 00
       A2 04              context specific explicit tag 2, length 4
          04 02 12 34     universal tag 4, length 2, value 1234
    

    如果省略了可选字段,则不存在相应的编码,例如如果我们省略 myText1(注意 myText2 不是可选的):

    30 09                 SEQUENCE of length 9
       81 01 00           context specific implicit tag 1, length 1, value 00
       A2 04              context specific explicit tag 2, length 4
          04 02 12 34     universal tag 4, length 2, value 1234
    

    在对序列值和显式标记的 myText2 值进行编码时,请注意构造形式位的使用。进一步注意隐式和显式标记之间的区别。

    【讨论】:

    • 上述示例中的C2必须是A230 0D 80 02 01 01 81 01 00 A2 04 04 02 12 34
    猜你喜欢
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 2022-01-22
    • 2012-04-11
    • 1970-01-01
    相关资源
    最近更新 更多