我有一小部分代码:
from pyasn1.type import univ
from pyasn1.codec.ber import decoder
decoder.decode(binary_file.read(5))我的binary_file变量是一个特殊的二进制文件编码(CDR)
如果我尝试解码已读取的部分,它会显示以下错误:
pyasn1.error.PyAsn1Error: [128:0:0]+[128:32:79] not in asn1Spec: None我该怎么修复呢?
发布于 2016-07-13 06:26:45
除非您正在解码仅包含基本ASN.1类型(如整数、序列等)的数据结构,否则您需要将顶级ASN.1数据结构对象传递给解码器。这样,解码器可以将自定义标记( BER/DER/CER序列化中的TLV元组)与数据结构对象中存在的相同标记进行匹配。例如:
custom_int_type = Integer().subtype(implicitTag=Tag(tagClassContext, tagFormatSimple, 40))
custom_int_instance = custom_int_type.clone(12345)
serialization = encode(custom_int_instance)
# this will fail on unknown custom ASN.1 type tag
custom_int_instance, rest_of_serialization = decode(serialization)
# this will succeed as custom ASN.1 type (containing tag) is provided
custom_int_instance, rest_of_serialization = decode(serialization, asn1Spec=custom_int_type)这里有一个pyasn1 documentation on decoders的链接。
要将ASN.1语法传递给pyasn1解码器,你必须首先将你的语法转换成pyasn1/Python对象树。这是一次性操作,有时可以使用asn1late工具自动执行。
我的另一个担忧是,您可能正在读取序列化数据的一小部分(5个八位字节)。如果您的数据是使用“无限长度编码模式”序列化的,那么这可能是一个有效的操作,否则解码器可能会在输入不足时失败。
https://stackoverflow.com/questions/38307884
复制相似问题