简体   繁体   English

如何在Python中使用ASN1库解码.BER

[英]How to decode .BER using ASN1 Library in Python

I have an encoded file with .BER format that I can't decode. 我有一个无法解码的.BER格式的编码文件。

After searching, I got that I need to use ASN1 library to be able to decode this file and to be readable. 搜索之后,我发现我需要使用ASN1库才能解码此文件并使其可读。

Can someone guide me how to decode .BER file using ASN1 library on python? 有人可以指导我如何使用python上的ASN1库解码.BER文件吗?

I have checked online and found that there are some online ASN1 decoder that correctly decode my file. 我已经在线检查过,发现有一些在线ASN1解码器可以正确解码我的文件。 However, I won't be able to use them as I need to decode it on my own not using an online tool. 但是,我将无法使用它们,因为我需要不使用在线工具自行对其进行解码。 I don't know how to get this tag and value in the script below. 我不知道如何在下面的脚本中获取此标记和值。

import asn1 import re import os import future Input_directory=open("D:\\Encoded_Nokia.ber","rb") 导入asn1导入重新导入os导入将来Input_directory = open(“ D:\\ Encoded_Nokia.ber”,“ rb”)

for lines in Input_directory: decoder = asn1.Decoder() output=decoder.start(lines) tag,output=decoder.read() 对于Input_directory中的行:解码器= asn1.Decoder()输出= decoder.start(行) tag,output=decoder.read()

Those are the errors i got: 这些是我得到的错误:

  File "C:\Python34\lib\site-packages\asn1.py", line 421, in read
    value = self._read_value(tag.nr, length)
  File "C:\Python34\lib\site-packages\asn1.py", line 508, in _read_value
    bytes_data = self._read_bytes(length)
  File "C:\Python34\lib\site-packages\asn1.py", line 541, in _read_bytes
    raise Error('Premature end of input.')
asn1.Error: Premature end of input.

BER may use an indefinite length encoding for some constructed data values. BER可以对某些构造的数据值使用不确定长度的编码。 The error you got suggests that you passed truncated bytes into the decoder. 您得到的错误表明您将截断的字节传递给了解码器。 This means that you should pass the entire content of the decoder like this: 这意味着您应该像这样传递解码器的全部内容:

Input_directory=open("D:\Encoded_Nokia.ber","rb")

# reads everything
content = Input_directory.read()
output=decoder.start(content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM