【问题标题】:Declaring type *big.Int overflowing constant golang声明类型 *big.Int 溢出常量 golang
【发布时间】:2016-09-26 21:28:11
【问题描述】:

使用 crypto/rsa 包生成密钥对很简单,但自己声明却很痛苦。

我正在尝试声明一个 rsa.PublicKey 类型的变量,其定义为:

type PublicKey struct {
        N *big.Int // modulus
        E int      // public exponent
}

我已经尝试了一百零三种方法,但我的代码目前看起来像:

PublicKey := new(rsa.PublicKey)

PublicKey.N = 816296297763124917516388440338759500423535395290623239231731567955308683122890408110917894172120047293936355563865250296188045077627313515614945465389856882915690164742049821466713295090362914686221827012330520911241180940331170800129566133563943306086709509374426793735798983196271063876215936717347200817820685489907456621846519078704338901417077754153251584919148131668369473222078960469749879767829241702858598298315759777245767370065542249841401685747514693845945420663931515035586797756896017462499826100826469085345198490755785708882569397123671313993933597159332140624225622926365258472081852103795720495728779491860405409429756519754432759030127289255409541378096471189783136441306888685144178712329637014132885623358066824356187044819578205172698506597932578231190886063535262514544569054747443504586895362356519252402500104155389876467086444850150261677007183689594568339805440756958346151465691221654766132846717117978938197863452998630570321897641091974200100764524637808876854013287571133384735164135339783262769321526997252096927500094807456840263828514476848496064212933462545940124330314474126636272733109787671957872657823289210871868354885730638093964892263227561606744192311648252923260065974092128743259979645799

PublicKey.E = 65537

我收到此错误:

./main.go:21: constant too large: 816296297763124917516388440338759500423535395290623239231731567955308683122890408110917894172120047293936355563865250296188045077627313515614945465389856882915690164742049821466713295090362914686221827012330520911241180940331170800129566133563943306086709509374426793735798983196271063876215936717347200817820685489907456621846519078704338901417077754153251584919148131668369473222078960469749879767829241702858598298315759777245767370065542249841401685747514693845945420663931515035586797756896017462499826100826469085345198490755785708882569397123671313993933597159332140624225622926365258472081852103795720495728779491860405409429756519754432759030127289255409541378096471189783136441306888685144178712329637014132885623358066824356187044819578205172698506597932578231190886063535262514544569054747443504586895362356519252402500104155389876467086444850150261677007183689594568339805440756958346151465691221654766132846717117978938197863452998630570321897641091974200100764524637808876854013287571133384735164135339783262769321526997252096927500094807456840263828514476848496064212933462545940124330314474126636272733109787671957872657823289210871868354885730638093964892263227561606744192311648252923260065974092128743259979645799
./main.go:21: overflow in constant
./main.go:21: cannot use 0 (type int) as type *big.Int in assignment

有什么帮助吗?

【问题讨论】:

    标签: go struct rsa bigint


    【解决方案1】:

    问题在于 Go 将您的文字值解释为 int。但是,它并不真正适合 32 位,所以它失败了。但是,您可以在 big.Int 创建后设置其值:

    PublicKey.N = big.NewInt(0)
    PublicKey.N.SetBytes([]byte("8162962977631249175163884403387595004235353952981629629776312491751638844033875950042353539529"))
    fmt.Println(N)
    // 8162962977631249175163884403387595004235353952981629629776312491751638844033875950042353539529
    

    【讨论】:

      【解决方案2】:

      T. Claverie 的答案将创建一个公钥,但不会将模数 (PublicKey.N) 设置为您看到的数字。相反,它将使用这些数字的字符代码。

      fmt.Print([]byte("1"))
      [49]
      

      您可以使用 big.Int.UnmarshalText() 来获取您在输入字符串中看到的键。

      PublicKey := new(rsa.PublicKey)
      PublicKey.N = big.NewInt(0)
      PublicKey.N.UnmarshalText([]byte("8162962977631249175163884403387595004235353952981629629776312491751638844033875950042353539529"))
      

      您可以在这里看到不同之处:https://play.golang.org/p/HL6YNdcSubv

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-17
        • 1970-01-01
        • 1970-01-01
        • 2021-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        相关资源
        最近更新 更多