卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35666本站已运行398

解组 asn 失败

解组 asn 失败

php小编西瓜在这里与大家分享一个关于“解组 asn 失败”的问题。在网络通信中,ASN(Autonomous System Number)是用来标识自治系统的数字,但有时在解组ASN时会出现解组失败的情况。这可能是由于ASN编码格式错误、ASN数据包损坏或解析器不兼容等原因导致的。在本文中,我们将探讨解组ASN失败的原因和解决方法,帮助大家更好地理解和解决这个问题。

问题内容

package main

import (
    "encoding/asn1"
    "fmt"
)

type SimpleStruct struct {
    Value int
}

func main() {
    berBytes := []byte{0x02, 0x01, 0x05}

    var simpleStruct SimpleStruct
    _, err := asn1.Unmarshal(berBytes, &simpleStruct)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Printf("Decoded value: %dn", simpleStruct.Value)
}

我试图解组但出现以下错误:


Error: asn1: structure error: tags don't match (16 vs {class:0 tag:2 length:1 isCompound:false}) {optional:false explicit:false application:false private:false defaultValue: tag: stringType:0 timeType:0 set:false omitEmpty:false} SimpleStruct @2

有人可以帮忙吗?谢谢

解决方法

0x020105 编码整数 5(参见 https://www.php.cn/link/8ae7733f9bc11275e8d0a0fdabe5be0a ),因此应该将其解组为整数,而不是具有整数字段的结构:

package main

import (
    "encoding/asn1"
    "fmt"
)

func main() {
    berBytes := []byte{0x02, 0x01, 0x05}

    var v int
    _, err := asn1.Unmarshal(berBytes, &v)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Printf("Decoded value: %dn", v)
    // Output:
    //   Decoded value: 5
}

并且 SimpleStruct{Value: 5} 被编组为 0x3003020105:

package main

import (
    "encoding/asn1"
    "fmt"
)

type SimpleStruct struct {
    Value int
}

func main() {
    simpleStruct := SimpleStruct{Value: 5}
    buf, err := asn1.Marshal(simpleStruct)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Printf("Encoded value: 0x%xn", buf)
    // Output:
    //   Encoded value: 0x3003020105
}

卓越飞翔博客
上一篇: gomobile:iOS 上的错误返回值同时具有 NSError 和布尔返回值
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏