encode - 手动读取编码asn文件

标签 encode asn.1 tlv ber

  1. 如何手动读取编码asn文件?
  2. 什么是标签长度值,有好的教程吗?

在下面的示例中,我阅读了它们中的每一个,但对我来说并不干净,任何人都可以帮助阅读它们中的每一个:

  1. 30 82 02 10 04 01 56 …(更多字节)

The first byte is 00110000 in binary. The first two bits are 00 so the class is again 0. The third bit is 1, so it is structured. The last five bits are 10000, so the tag is 16 decimal. The next byte is 82 hex, which is 130 decimal, which is 128 + 2, the the following 2 bytes give the length. They are 02 10, which is interpreted in “big-endian” format as 2*256 + 16 = 528. The next 528 bytes, starting with 04 01 56, contain the contents.

  • df 82 02 05 12 34 56 78 90
  • The first byte is 11011111 in binary. The first two bits are 11, so this is class 3 – Private. The next bit is a 0, so this is primitive. The remaining five bits are all 1, so the actual tag starts in the second byte. The second byte has a leading one, and the third byte does not, so the tag is constructed by taking those two bytes (10000010 00000010 in binary), dropping their leading bits to get the fourteen bits 00000100000010, and interpreting this as a binary number. Thus, the tag is 258 decimal. The next byte is 05, which is less than 128, so that is the actual length of the contents. The next 5 bytes (12 34 56 78 90) are the contents.

  • 30 80 04 03 56 78 90 00 00
  • The first byte, 30, is one we’ve seen before. It is universal class, structured, with tag 16. The next byte is 80, so the length is unknown at first. The contents are all the following bytes, up to (but not including) the first two sequential zero bytes. So the contents are 04 03 56 78 90, and we can figure out from the contents that the length is 5.

    Examples Reference

    最佳答案

    每个值在编码中表示为标签-长度-值三元组。

    标签在值的 ASN.1 数据定义中定义,对于识别值的类型非常重要,以防存在多种可能性(例如 CHOICE 值或可选值)。

    长度以字节为单位编码正确值部分的长度。

    该值是传输值的编码。使用的编码根据值的类型而变化(例如,字符串使用与整数或对象标识符不同的编码)。一般来说,您需要 ASN.1 定义才能理解值字节。对于复合值,值部分中有零个或多个完整的值编码(每个都带有标记长度值),例如 SEQUENCE 值的各个字段。

    Google 搜索 ASN.1 BER 编码会显示大量 Material ,例如 this one .

    顺便说一句:你的第三个例子有点误导。不定长度值中的字节流需要拆分为封闭的值(其中可能包含字节序列 00 00)。仅当您在需要标记的位置找到 00 00 时,这才是值结束的信号。因此需要分析内容04 03 56 78 90(标签04,长度03,值56 78 90)以确保接下来的00 00表示结束。

    编辑:

    Interpretation of the first example:
    tag 30: bits 00 (UNIVERSAL)   1 (compound)    10000 (tag 16)
    length 82 02 10: bits 1 (long length encoding) 0000010 (length uses 2 bytes)
            00000010 00010000 (length in binary, 528 decimal)
    
    Interpretation of the second example:
    tag df 82 02: bits 11 (PRIVATE) 0 (primitive) 11111 (long tag encoding)
            1 (there is a further tag byte) 0000010 (value 2)
            0 (this is the last tag byte) 0000010 (value 2)
            total tag value therefore 2*128 + 2 = 258
    length 05: bits 0 (short length encoding) 0000101 (length in binary, decimal 5)
    
    Interpretation of the third example:
    tag 30: bits 00 (UNIVERSAL) 1 (compound) 10000 (tag value 16)
    length 80: indefinite length, so we must look at the contained values
        tag 04: bits 00 (UNIVERSAL) 0 (primitive) 00100 (tag value 4)
        length 03: bits 0 (short length encoding) 0000011 (length value 3)
        value 56 78 90
    
        tag 00: signals end of indefinite length content as there is no
                UNIVERSAL tag 0
        length 00: dummy length byte of end marker
    

    关于encode - 手动读取编码asn文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34767067/

    相关文章:

    Java 和 RFC 3986 URI 编码

    php - PHP 中 JavaScript 的 decodeURI 组件的等价物是什么?

    x509 - 将 X.509 证书从 DER 编码转换为 PER 编码

    java - 如何在 java 中使用 bouncycaSTLe 解析 ASN.1 对象并获取数据

    php - Onlinecity SMPP - 添加新的 tlv 参数

    python - 为什么我的代码显示为困惑而实际上却没有?

    带有字符串和 UIImages 的 Swift Codable 协议(protocol)

    javascript - 生成 1024 位 RSA key 对并将公钥以 ASN.1 格式(十六进制)保存在 node.js 中

    php - 如何将字符串中的两个字符视为一个字节?