Go - 如何从字符串设置 RSA 公钥模数?

我正在尝试使用 Go 的RSA包加密密码。


这是我到目前为止所拥有的:


package main


import (

    "fmt"

    "time"

    "net/http"

    "strconv"

    "io/ioutil"

    "encoding/json"

    "errors"

    "crypto/rsa"

    "crypto/rand"

    //"math/big"

)


func main() {

    if err := Login("username", "password"); err != nil {

        fmt.Println(err)

    }

}


func Login(username, password string) error {

    doNotCache := strconv.FormatInt(time.Now().UnixNano() / int64(time.Millisecond), 10)


    // Get RSA Key

    resp, err := http.PostForm("https://steamcommunity.com/login/getrsakey/", map[string][]string{

        "donotcache": {doNotCache},

        "username": {username},

    })

    if err != nil {

        return err

    }


    content, err := ioutil.ReadAll(resp.Body)

    if err != nil {

        return err

    }


    var decoded map[string]interface{}

    err = json.Unmarshal(content, &decoded)

    if err != nil {

        return err

    }


    if decoded["success"] != true {

        return errors.New("Failed to retrieve RSA key.")

    }


    // Set encryption variables

    var privateKey *rsa.PrivateKey

    var publicKey *rsa.PublicKey

    var plain_text, encrypted []byte


    plain_text = []byte(password)


    // Generate Private Key

    if privateKey, err = rsa.GenerateKey(rand.Reader, 1024); err != nil {

        return err

    }


    privateKey.Precompute()


    if err = privateKey.Validate(); err != nil {

        return err

    }


    publicKey.N = decoded["publickey_mod"].(string) // <- This is not right, I need to create a modulus from the publickey_mod string and it needs to be of type big.Int

    publicKey.E = decoded["publickey_exp"].(int)


    encrypted, err = rsa.EncryptPKCS1v15(rand.Reader, publicKey, plain_text)

    if err != nil {

        return err

    }


    fmt.Printf("PKCS1 Encrypted [%s] to \n[%x]\n", string(plain_text), encrypted)


    return nil

}

我无法从给定的字符串将 publicKey.N 值设置为 big.Int。


和变量decoded["publickey_mod"]看起来像010001


我正在尝试为https://steamcommunity.com/加密此密码。


有只小跳蛙
浏览 107回答 1
1回答

动漫人物

decoded["publickey_mod"] 是十六进制字符串,您需要将其转换为 big.Int:publicKey.N, _ = new(big.Int).SetString(decoded["publickey_mod"].(string), 16 /* = base 16 */)// json numbers are float64 by default unless you use a struct and force a typepublicKey.E = int(decoded["publickey_exp"].(float64))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go