存储和检索RSA加密密钥

huangapple go评论80阅读模式
英文:

Storing and retrieving RSA encryption key

问题

我正在尝试构建一个API,但为了正确保护它,我认为我需要使用RSA加密来存储在我的服务器上的私钥和客户端的公钥。我已经将生成的私钥存储到一个JSON文件中,计划将其存储在我的服务器上,但是为了将其写入JSON,我需要将类型转换为[]byte。现在,当我尝试检索私钥以生成公钥时,它不允许我使用*Publickey类型的字节。

我能想到的唯一其他方法是种子随机数生成器,这样我就可以在服务器上拥有一个秘密种子,然后我的私钥应该始终生成相同的内容。对此有任何帮助将非常好。

package main

import (
	"bytes"
	"crypto/rand"
	"crypto/rsa"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	mimicPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	buf := new(bytes.Buffer)
	json.NewEncoder(buf).Encode(mimicPrivateKey)
	secrets, _ := os.OpenFile("secrets.json", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
	// Close the secrets file when the surrounding function exists

	secrets.WriteString(buf.String())
	secrets.Close()

	secrets, _ = os.OpenFile("secrets.json", os.O_RDWR, 0666)
	serverKey, _ := ioutil.ReadAll(secrets)
	if serverKey != nil {
		fmt.Println("can not open key")
	}

	serverKeyPublic := &serverKey.PublicKey
}
英文:

I am trying to build an API, but to secure it properly I believe I need to go with RSA encryption for a private key stored on my server and a public key for the client. I have stored the generated private key into a JSON file, I plan to store on my server but to write to JSON, I needed to convert the type too []byte. Now when I try to retrieve the private key to generate a public key, but it will not let me use type bytes for *Publickey
The only other way I can think of to accomplish this goal is to seed the random number generator, so I can have the seed a secret on my server and then my private key should always generate to the same thing, any help this this would be great.

package main

import (
    "bytes"
    "crypto/rand"
    "crypto/rsa"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)

func main() {
    mimicPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
    if err != nil {
	    fmt.Println(err)
	    os.Exit(1)
    }
    buf := new(bytes.Buffer)
    json.NewEncoder(buf).Encode(mimicPrivateKey)
    secrets, _ := os.OpenFile("secrets.json",    os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
    // Close the secrets file when the surrounding function exists

    secrets.WriteString(buf.String())
    secrets.Close()

    secrets, _ = os.OpenFile("secrets.json", os.O_RDWR, 0666)
    serverKey, _ := ioutil.ReadAll(secrets)
    if serverKey != nil {
	    fmt.Println("can not open key")
    }

    serverKeyPublic := &serverKey.PublicKey
}

答案1

得分: 2

你需要对其进行解组:

var data *rsa.PrivateKey
err = json.Unmarshal(serverKey, &data)
if err != nil {
    panic(err)
}

你可以使用以下代码:

err = ioutil.WriteFile("secrets.json", buf.Bytes(), 0666)

以及

serverKey, err := ioutil.ReadFile("secrets.json")

请参考以下代码:

package main

import (
    "bytes"
    "crypto/rand"
    "crypto/rsa"
    "encoding/json"
    "fmt"
    "io/ioutil"
)

func main() {
    mimicPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
    if err != nil {
        panic(err)
    }
    var buf bytes.Buffer
    err = json.NewEncoder(&buf).Encode(mimicPrivateKey)
    if err != nil {
        panic(err)
    }
    err = ioutil.WriteFile("secrets.json", buf.Bytes(), 0666)
    if err != nil {
        panic(err)
    }

    serverKey, err := ioutil.ReadFile("secrets.json")
    if err != nil {
        panic(err)
    }
    var data *rsa.PrivateKey
    err = json.Unmarshal(serverKey, &data)
    if err != nil {
        panic(err)
    }
    serverKeyPublic := data.PublicKey
    fmt.Println(serverKeyPublic)
}
英文:

You need to Unmarshal it:

var data *rsa.PrivateKey
err = json.Unmarshal(serverKey, &data)
if err != nil {
	panic(err)
}

And you may use

err = ioutil.WriteFile("secrets.json", buf.Bytes(), 0666)

and

serverKey, err := ioutil.ReadFile("secrets.json")

See:

package main

import (
	"bytes"
	"crypto/rand"
	"crypto/rsa"
	"encoding/json"
	"fmt"
	"io/ioutil"
)

func main() {
	mimicPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		panic(err)
	}
	var buf bytes.Buffer
	err = json.NewEncoder(&buf).Encode(mimicPrivateKey)
	if err != nil {
		panic(err)
	}
	err = ioutil.WriteFile("secrets.json", buf.Bytes(), 0666)
	if err != nil {
		panic(err)
	}

	serverKey, err := ioutil.ReadFile("secrets.json")
	if err != nil {
		panic(err)
	}
	var data *rsa.PrivateKey
	err = json.Unmarshal(serverKey, &data)
	if err != nil {
		panic(err)
	}
	serverKeyPublic := data.PublicKey
	fmt.Println(serverKeyPublic)
}

huangapple
  • 本文由 发表于 2016年10月25日 13:10:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/40231805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定