如何使用pyasn1解码ASN1编码的数据(嵌套结构化数据)?

2024-06-09 21:28:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据应该可以如下方式获取。。

示例结构:-

    listOfVolumes: -> SequenceOf
         ChangeOfCharCondition -> Sequence
                dataUplink: 9612742 -> Integer   
                dataDownlink: 216449 -> Integer
                changeCondition: qoSChange (0) -> Enumerated
                Time: 1206202320082b0530 -> OctetString

         ChangeOfCharCondition -> Sequence
                qosNegotiated: 0223921f9396979774f9ffff -> OctetString
                dataUplink: 57664480 -> Integer
                dataDownlink: 1460443 -> Integer
                changeCondition: recordClosure (2) -> Enumerated
                Time: 1206210017072b0530 -> OctetString

如何解码以这种特定格式编码的数据(bytearray)?

我可以解码,如果它只是结构序列中的一个序列, 但我很难把数据循环一遍,能不能请任何人 建议我一个更好的方法来解决这个问题?任何建议对我都有价值。。 提前到达..
示例代码:

class ChangeCondition(univ.Enumerated):

     namedValues = namedval.NamedValues(
        ('qoS', 0),
        ('Time', 1),
        ('Closure', 2),
        ('ContinueOngoing', 3),
        ('RetryandTerminateOngoing', 4),
        ('TerminateOngoing', 5),
        ('cGI', 6),
        ('rAI', 7),
        ('dT', 8),
        ('dT-Removal', 9))
        subtypeSpec = univ.Enumerated.subtypeSpec + \
                constraint.SingleValueConstraint(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

class ChangeOfCharCondition(univ.Sequence):

    componentType = namedtype.NamedTypes(
    namedtype.OptionalNamedType('Negotiated', univ.OctetString().subtype(
        implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2))),
    namedtype.OptionalNamedType('dataUplink', univ.Integer().subtype(
        implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))),
    namedtype.OptionalNamedType('dataDownlink', univ.Integer().subtype(
        implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))),
    namedtype.NamedType('changeCondition', ChangeCondition().subtype(
        implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 5))),
    namedtype.OptionalNamedType('Time', univ.OctetString().subtype(
        implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 6)))
       )

class ListOfVolumes(univ.SequenceOf):

    tagSet = baseTagSet = tag.initTagSet(tag.Tag(tag.tagClassContext,tag.tagFormatSimple, 12),)
    componentType = ChangeOfCharCondition()

class MyCdr(univ.Set):

     tagSet = baseTagSet = tag.initTagSet(tag.Tag(tag.tagClassContext,tag.tagFormatSimple, 21))
    componentType = namedtype.NamedTypes(
    namedtype.OptionalNamedType('listOfVolumes', ListOfVolumes()))                                                    

我的数据如下

bytearray(b'\xb5\x81\x2a\xac(0&\xa2\x0e\x81\x0c\x01#Q\x1f\x93\x96HHt\xf9\xff\xff\x83\x02\x06x\x84\x02\x13m\x85\x01\x02\x86\t6\x05"#\x12E+\x050')

Tags: 数据timetagintegerunivsubtypeoctetstringnamedtype
2条回答

如果您对所有这些数据结构都有正式的ASN.1语法,那么在这里发布它会很有帮助。

你的一般方法看起来是正确的,但是你做ASN.1标记的方式是可疑的。

从最初对语法的描述来看,您应该能够通过调用pyasn1来解码bytearray:

decoder.decode(mybytearray, asn1Spec=ListOfVolumes())

例如,将ListOfVolumes()类实例作为顶级原型对象传递给解码器。如果失败,可能是由于不正确的标记。

要深入研究,可能有助于启用pyasn1调试:

from pyasn1 import debug
debug.setLogger(debug.Debug('all')

并查看从bytearray读取的标记以及规范中的哪些对象与它们匹配。

如果您的编码包含一个有效的项序列,并且您相应地定义了pyasn1数据结构,那么pyasn1解码器应该能够独自循环所有实例序列。

你能发布你的数据结构的pyasn1规范吗?

相关问题 更多 >