2016-02-04 60 views
1

我对ANS1编码非常陌生,我需要一些指导。如何使用ASN.1进行编码?

......假设我有以下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

回答

2

假设我们想编码与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 
    C2 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 
    C2 04    context specific explicit tag 2, length 4 
     04 02 12 34  universal tag 4, length 2, value 1234 

注编码序列值和显式时所构造的表格位的使用标记myText2值。进一步注意隐式和显式标签之间的区别。